Sunday, March 20, 2016

Detect phone type and network type

Android example to detect phone (CDMA, GSM, SIP or NONE) type and network type (CDMA, EDGE, GPRS, LTE...etc).

MainActivity.java
package com.blogspot.android_er.androidphone;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textPhoneType = (TextView)findViewById(R.id.textphonetype);
        TextView textNetworkType = (TextView)findViewById(R.id.textnetworktype);

        TelephonyManager telephonyManager =
                (TelephonyManager) getApplicationContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);

        if(telephonyManager == null){
            textPhoneType.setText("not supported TELEPHONY_SERVICE");
        }else{
            int phoneType = telephonyManager.getPhoneType();
            int networkType = telephonyManager.getNetworkType();

            textPhoneType.setText(
                    "Phone Type: " + phoneType + " " + decPhoneType(phoneType));
            textNetworkType.setText(
                    "Network Type: " + networkType + " " + decNetworkType(networkType));
        }
    }

    private String decPhoneType(int type){
        String result = "";

        switch(type){
            case TelephonyManager.PHONE_TYPE_CDMA:
                result = "CDMA";
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                result = "GSM";
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                result = "NONE";
                break;
            case TelephonyManager.PHONE_TYPE_SIP:
                result = "SLIP";
                break;
            default:
                result = "unknown type";
        }
        return result;
    }

    private String decNetworkType(int type){
        String result = "";

        switch(type){
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                result = "1xRTT";
                break;
            case TelephonyManager.NETWORK_TYPE_CDMA:
                result = "CDMA";
                break;
            case TelephonyManager.NETWORK_TYPE_EDGE:
                result = "EDGE";
                break;
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                result = "EHRPD";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                result = "EVDO_0";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                result = "EVDO_A";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                result = "EVDO_B";
                break;
            case TelephonyManager.NETWORK_TYPE_GPRS:
                result = "GPRS";
                break;
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                result = "HSDPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPA:
                result = "HSPA";
                break;
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                result = "HSPAP";
                break;
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                result = "HSUPA";
                break;
            case TelephonyManager.NETWORK_TYPE_IDEN:
                result = "IDEN";
                break;
            case TelephonyManager.NETWORK_TYPE_LTE:
                result = "LTE";
                break;
            case TelephonyManager.NETWORK_TYPE_UMTS:
                result = "UMTS";
                break;
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                result = "UNKNOWN";
                break;
            default:
                result = "unknown type";
        }
        return result;
    }
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context="com.blogspot.android_er.androidphone.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textphonetype"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textnetworktype"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>



No comments: