Saturday, May 6, 2017
Detect phone type and network type
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
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>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.