易采站长站为您分析Android中TelephonyManager用法,结合实例形式分析了TelephonyManager类的功能,使用技巧与相关注意事项,需要的朋友可以参考下
本文实例讲述了Android中TelephonyManager用法。,具体如下:
一、概述:
TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法。其中包括手机SIM的状态和信息、电信网络的状态及手机用户的信息。在应用程序中可以使用这些get方法获取相关数据。
TelephonyManager类的对象可以通过Context.getSystemService(Context.TELEPHONY_SERVICE)方法来获得,需要注意的是有些通讯信息的获取对应用程序的权限有一定的限制,在开发的时候需要为其添加相应的权限。
二、示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 读取sim卡
TelephonyManager tm = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
//
// String tel = tm.getLine1Number(); // 取出MSISDN,很可能为空
// String iccid = tm.getSimSerialNumber(); // 取出ICCID
// String imsi = tm.getSubscriberId(); // 取出IMSI
System.out.println(tm.getCallState());
System.out.println(tm.getDataActivity());
System.out.println(tm.getDataState());
System.out.println("得到设备的ID,IMEI或者MEID:" + tm.getDeviceId());
System.out.println("软件版本:"+tm.getDeviceSoftwareVersion());
if (tm.getLine1Number()!=null) {
System.out.println("电话号码:"+tm.getLine1Number());
} else {
System.out.println("电话号码为空");
}
System.out.println("电信网路国别:" + tm.getNetworkCountryIso()); // 电信网路国别
System.out.println("电信公司代号:" + tm.getNetworkOperator());
System.out.println("电信公司名称:" + tm.getNetworkOperatorName());
// System.out.println(tm.getNetworkType());
// 行动网路类型
String[] networkTypeArray = { "UNKNOWN", "GPRS", "EDGE", "UMTS",
"CDMA", "EVDO 0", "EVDO A", "1xRTT", "HSDPA", "HSUPA", "HSPA" };
String networkType = networkTypeArray[tm.getNetworkType()];
System.out.println("行动网路类型:"+networkType);
// System.out.println(tm.getPhoneType());
// 行动通讯类型
String[] phoneTypeArray = {"NONE", "GSM", "CDMA"};
String phoneType = phoneTypeArray[tm.getPhoneType()];
System.out.println("行动通讯类型:"+phoneType);
System.out.println("sim国家代码:"+tm.getSimCountryIso());
System.out.println(tm.getSimOperator());
System.out.println(tm.getSimOperatorName());
// System.out.println(tm.getSimSerialNumber());
System.out.println(tm.getSimState());
// System.out.println(tm.getSubscriberId()); // 手机 IMSI
System.out.println(tm.getVoiceMailAlphaTag());
// System.out.println("得到位置信息,主要是当前注册小区的位置码:"+tm.getCellLocation());
// System.out.println(tm.getNeighboringCellInfo());
// 手机漫游状态
String roamingStatus = tm.isNetworkRoaming() ? "漫游中" : "非漫游";
System.out.println(roamingStatus);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}










