通过getSystemService()获取TelephonyManager对象,再获取电信网络的相关信息
通过android.provider.Settings.System.getString()获取手机相关的设置信息
示例代码如下:
private ArrayList<String> titles; private ArrayList<String> contents; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); titles = new ArrayList<String>(); contents = new ArrayList<String>(); addItem("手机电话号码", tm.getLine1Number()); addItem("电信网络国别", tm.getNetworkCountryIso()); addItem("电信公司代码", tm.getNetworkOperator()); addItem("电信公司名称", tm.getNetworkOperatorName()); titles.add("行动通讯类型"); switch (tm.getPhoneType()) { case TelephonyManager.PHONE_TYPE_CDMA: contents.add("CDMA"); break; case TelephonyManager.PHONE_TYPE_GSM: contents.add("GSM"); break; case TelephonyManager.PHONE_TYPE_NONE: contents.add("无"); break; case TelephonyManager.PHONE_TYPE_SIP: contents.add("SIP"); break; default: contents.add("未知"); break; } titles.add("网络类型"); switch (tm.getNetworkType()) { case TelephonyManager.NETWORK_TYPE_EDGE: contents.add("EDGE"); break; case TelephonyManager.NETWORK_TYPE_CDMA: contents.add("CDMA"); break; case TelephonyManager.NETWORK_TYPE_GPRS: contents.add("GPRS"); break; case TelephonyManager.NETWORK_TYPE_UMTS: contents.add("UMTS"); break; case TelephonyManager.NETWORK_TYPE_HSDPA: contents.add("HSDPA"); break; default: contents.add("未知"); break; } titles.add("手机漫游状态"); if (tm.isNetworkRoaming()) { contents.add("漫游中"); } else { contents.add("无漫游"); } titles.add("手机IMEI"); contents.add(tm.getDeviceId()); addItem("IMEI SV", tm.getLine1Number()); addItem("手机IMSI", tm.getLine1Number()); ContentResolver cr = getContentResolver(); titles.add("蓝牙状态"); String tmpStr = android.provider.Settings.System.getString(cr, android.provider.Settings.System.BLUETOOTH_ON); if (tmpStr != null && "1".equals(tmpStr)) { contents.add("已开启"); } else { contents.add("未开启"); } titles.add("WIFI状态"); tmpStr = android.provider.Settings.System.getString(cr, android.provider.Settings.System.WIFI_ON); if (tmpStr != null && "1".equals(tmpStr)) { contents.add("已开启"); } else { contents.add("未开启"); } titles.add("飞行模式"); tmpStr = android.provider.Settings.System.getString(cr, android.provider.Settings.System.AIRPLANE_MODE_ON); if (tmpStr != null && "1".equals(tmpStr)) { contents.add("已开启"); } else { contents.add("未开启"); } titles.add("数据漫游"); tmpStr = android.provider.Settings.System.getString(cr, android.provider.Settings.System.DATA_ROAMING); if (tmpStr != null && "1".equals(tmpStr)) { contents.add("已开启"); } else { contents.add("未开启"); } ((ListView) findViewById(R.id.lv)).setAdapter(new MyAdapter( MainActivity.this, titles, contents)); } private void addItem(String title, String content) { titles.add(title); if (content != null && !"".equals(content)) { contents.add(content); } else { contents.add("无法取得"); } }
实验发现:并不是每家电信业的手机号码都能够顺利获取的,所以,TelephonyManager.getLine1Number()有可能获取不到手机号码。