<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/phone_type" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/network_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/sim_state" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/network_type" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class NetworkDetector extends Activity {
//SIM卡状态常量
private static final String SIM_ABSENT = "Absent"; //手机内无SIM卡
private static final String SIM_READY = "Ready"; //SIM卡已准备好
private static final String SIM_PIN_REQUIRED = "PIN required"; //需要SIM卡的PIN解锁
private static final String SIM_PUK_REQUIRED = "PUK required"; //需要SIM卡的PUK解锁
private static final String SIM_NETWORK_LOCKED = "Network locked"; //需要Network PIN解锁
private static final String SIM_UNKNOWN = "Unknown"; //状态未知
//网络类型常量
private static final String NETWORK_CDMA = "CDMA: Either IS95A or IS95B (2G)";
private static final String NETWORK_EDGE = "EDGE (2.75G)";
private static final String NETWORK_GPRS = "GPRS (2.5G)";
private static final String NETWORK_UMTS = "UMTS (3G)";
private static final String NETWORK_EVDO_0 = "EVDO revision 0 (3G)";
private static final String NETWORK_EVDO_A = "EVDO revision A (3G - Transitional)";
private static final String NETWORK_EVDO_B = "EVDO revision B (3G - Transitional)";
private static final String NETWORK_1X_RTT = "1xRTT (2G - Transitional)";
private static final String NETWORK_HSDPA = "HSDPA (3G - Transitional)";
private static final String NETWORK_HSUPA = "HSUPA (3G - Transitional)";
private static final String NETWORK_HSPA = "HSPA (3G - Transitional)";
private static final String NETWORK_IDEN = "iDen (2G)";
private static final String NETWORK_UNKOWN = "Unknown";
//手机制式类型常量
private static final String PHONE_CDMA = "CDMA";
private static final String PHONE_GSM = "GSM";
private static final String PHONE_NONE = "No radio";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.network_detector);
//获取telephony系统服务,用于取得SIM卡和网络相关信息
final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//更新界面上相关网络信息
updateViews(tm);
//由于SIM卡状态和网络信息随时都可能发生变化,因此需要注册PhoneStateListener
//来实时更新界面显示的信息。这里监听两个事件:LISTEN_SERVICE_STATE和LISTEN_DATA_CONNECTION_STATE
//它们分别对应回调函数onServiceStateChanged和onDataConnectionStateChanged
tm.listen(new PhoneStateListener() {
@Override
public void onDataConnectionStateChanged(int state, int networkType) {
//数据连接状态改变可能导致网络类型的改变
updateViews(tm);
}
@Override
public void onServiceStateChanged(ServiceState serviceState) {
updateViews(tm);
}
}, PhoneStateListener.LISTEN_SERVICE_STATE
| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
}
@Override
protected void onResume() {
super.onResume();
final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
updateViews(tm);
}
/**
* 更新SIM卡状态和网络信息
* @param tm
*/
private final void updateViews(TelephonyManager tm) {
TextView view = null;
view = (TextView) findViewById(R.id.sim_state);
view.setText("SIM State : " + mapSimStateToName(tm.getSimState()));
view = (TextView) findViewById(R.id.network_type);
view.setText("Network Type : " + mapNetworkTypeToName(tm.getNetworkType()));
view = (TextView) findViewById(R.id.network_name);
view.setText("Network Operator : " + tm.getNetworkOperatorName());
view = (TextView) findViewById(R.id.phone_type);
view.setText("Phone Type : " + mapDeviceTypeToName(tm.getPhoneType()));
}
/**
* 将手机制式值以字符串形式返回
* @param phoneType
* @return
*/
private String mapDeviceTypeToName(int phoneType) {
switch (phoneType) {
case TelephonyManager.PHONE_TYPE_CDMA:
return PHONE_CDMA;
case TelephonyManager.PHONE_TYPE_GSM:
return PHONE_GSM;
case TelephonyManager.PHONE_TYPE_NONE:
return PHONE_NONE;
default:
//不应该走到这个分支
return null;
}
}
/**
* 将网络类型值以字符串形式返回
* @param networkType
* @return
*/
private String mapNetworkTypeToName(int networkType) {
switch (networkType) {
case TelephonyManager.NETWORK_TYPE_CDMA:
return NETWORK_CDMA;
case TelephonyManager.NETWORK_TYPE_EDGE:
return NETWORK_EDGE;
case TelephonyManager.NETWORK_TYPE_GPRS:
return NETWORK_GPRS;
case TelephonyManager.NETWORK_TYPE_UMTS:
return NETWORK_UMTS;
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return NETWORK_EVDO_0;
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return NETWORK_EVDO_A;
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return NETWORK_EVDO_B;
case TelephonyManager.NETWORK_TYPE_1xRTT:
return NETWORK_1X_RTT;
case TelephonyManager.NETWORK_TYPE_HSDPA:
return NETWORK_HSDPA;
case TelephonyManager.NETWORK_TYPE_HSPA:
return NETWORK_HSPA;
case TelephonyManager.NETWORK_TYPE_HSUPA:
return NETWORK_HSUPA;
case TelephonyManager.NETWORK_TYPE_IDEN:
return NETWORK_IDEN;
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return NETWORK_UNKOWN;
}
}
/**
* 将SIM卡状态值以字符串形式返回
* @param simState
* @return
*/
private static String mapSimStateToName(int simState) {
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
return SIM_ABSENT;
case TelephonyManager.SIM_STATE_READY:
return SIM_READY;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
return SIM_PIN_REQUIRED;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
return SIM_PUK_REQUIRED;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
return SIM_NETWORK_LOCKED;
case TelephonyManager.SIM_STATE_UNKNOWN:
return SIM_UNKNOWN;
default:
//不应该走到这个分支
return null;
}
}
}