TelephonyManager是一个管理手机通话状态,电话网络信息的服务类,提供了大量的getXxx()方法来获取电话网络的相关信息。
实例:获取网络和SIM卡信息
主界面文件包含一个ListView,用于显示获得的信息。
界面布局文件、数组资源文件略去。
TelephonyStatus.java代码:
package com.example.telephonystatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class TelephonyStatus extends Activity {
ListView showView;
//声明代表状态名的数组
String[] statusNames;
//声明代表手机状态的集合
ArrayList<String> statusValues = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telephony_status);
//获取系统的TelephonyManager对象
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//获取各种状态名称的数组
statusNames = getResources().getStringArray(R.array.statusNames);
//获取代表SIM卡状态的数组
//String[] simState = get
//获取设备编号
statusValues.add(telephonyManager.getDeviceId());
//获取系统平台的版本
statusValues.add(telephonyManager.getDeviceSoftwareVersion() != null ? telephonyManager.getDeviceSoftwareVersion() : "未知");
//获取网络运营商代号
statusValues.add(telephonyManager.getNetworkOperator());
//获取网络运营商名称
statusValues.add(telephonyManager.getNetworkOperatorName());
//获取设备所在的位置
statusValues.add(telephonyManager.getCellLocation() != null ? telephonyManager.getCellLocation().toString() : "未知位置");
//获取SIM卡的国别
statusValues.add(telephonyManager.getSimCountryIso());
//获取SIM卡的序列号
statusValues.add(telephonyManager.getSimSerialNumber());
//获得ListView对象
showView = (ListView) findViewById(R.id.listView);
ArrayList<Map<String, String>> status = new ArrayList<Map<String,String>>();
//遍历statusValues集合,将statusNames、statusValues的数据封装到List<Map<String,String>>集合中
for (int i = 0; i < statusValues.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", statusNames[i]);
map.put("value", statusValues.get(i));
status.add(map);
}
//使用SimpleAdapter封装List数据
SimpleAdapter adapter = new SimpleAdapter(this, status, R.layout.line, new String[]{"name","value"}, new int[]{R.id.name,R.id.value});
showView.setAdapter(adapter);
}
}