在AndroidManifest.xml文件中增加
<!--允許讀取電話狀態SIM的權限-->
<uses-permission
android:name="android.permission.READ_PHONE_STATE" />
代碼如下:
private
void getSystemPhoneMessage(){
TelephonyManager
telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//手機串號:GSM手機的
IMEI 和 CDMA手機的 MEID.
String
deviceID = telephonyManager.getDeviceId();
//手機號(有些手機號無法獲取,是因為運營商在SIM中沒有寫入手機號)
String
tel = telephonyManager.getLine1Number();
//獲取手機SIM卡的序列號
String
imei =telephonyManager.getSimSerialNumber();
//獲取客戶id,在gsm中是imsi號
String
imsi =telephonyManager.getSubscriberId();
//電話方位
CellLocation
str = telephonyManager.getCellLocation();
//運營商名稱,注意:僅當用戶已在網絡注冊時有效,在CDMA網絡中結果也許不可靠
String
networkoperatorName = telephonyManager.getNetworkOperatorName();
//取得和語音郵件相關的標簽,即為識別符
String
voiceMail = telephonyManager.getVoiceMailAlphaTag();
//獲取語音郵件號碼:
String
voiceMailNumber = telephonyManager.getVoiceMailNumber();
//獲取ISO國家碼,相當於提供SIM卡的國家碼。
String
simCountryIso = telephonyManager.getSimCountryIso();
/**
* 電話狀態:
* 1.tm.CALL_STATE_IDLE=0 無活動
* 2.tm.CALL_STATE_RINGING=1 響鈴
* 3.tm.CALL_STATE_OFFHOOK=2 摘機
*/
int
callState = telephonyManager.getCallState();
/**
* 設備的軟件版本號:
* 例如:the IMEI/SV(software version) for GSM phones.
* Return null if the software version is not available.
*/
String
devicesoftware = telephonyManager.getDeviceSoftwareVersion();
/**
* 獲取ISO標准的國家碼,即國際長途區號。
* 注意:僅當用戶已在網絡注冊后有效。
* 在CDMA網絡中結果也許不可靠。
*/
String
networkCountry = telephonyManager.getNetworkCountryIso();
/**
* MCC+MNC(mobile country code + mobile network code)
* 注意:僅當用戶已在網絡注冊時有效。
* 在CDMA網絡中結果也許不可靠。
*/
String
networkoperator = telephonyManager.getNetworkOperator();
/**
* 當前使用的網絡類型:
* 例如: NETWORK_TYPE_UNKNOWN 網絡類型未知 0
NETWORK_TYPE_GPRS GPRS網絡 1
NETWORK_TYPE_EDGE EDGE網絡 2
NETWORK_TYPE_UMTS UMTS網絡 3
NETWORK_TYPE_HSDPA HSDPA網絡 8
NETWORK_TYPE_HSUPA HSUPA網絡 9
NETWORK_TYPE_HSPA HSPA網絡 10
NETWORK_TYPE_CDMA CDMA網絡,IS95A 或 IS95B. 4
NETWORK_TYPE_EVDO_0 EVDO網絡, revision 0. 5
NETWORK_TYPE_EVDO_A EVDO網絡, revision A. 6
NETWORK_TYPE_1xRTT 1xRTT網絡 7
*/
int
netWorkType = telephonyManager.getNetworkType();
/**
* 手機類型:
* 例如: PHONE_TYPE_NONE 無信號
PHONE_TYPE_GSM GSM信號
PHONE_TYPE_CDMA CDMA信號
*/
int
phoneType = telephonyManager.getPhoneType();
/**
* 獲取SIM卡提供的移動國家碼和移動網絡碼.5或6位的十進制數字.
* SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
*/
String
simOperator = telephonyManager.getSimOperator();
/**
* 服務商名稱:
* 例如:中國移動、聯通
* SIM卡的狀態必須是 SIM_STATE_READY(使用getSimState()判斷).
*/
String
simOperatorName = telephonyManager.getSimOperatorName();
/**
* SIM的狀態信息:
* SIM_STATE_UNKNOWN 未知狀態 0
SIM_STATE_ABSENT 沒插卡 1
SIM_STATE_PIN_REQUIRED 鎖定狀態,需要用戶的PIN碼解鎖 2
SIM_STATE_PUK_REQUIRED 鎖定狀態,需要用戶的PUK碼解鎖 3
SIM_STATE_NETWORK_LOCKED 鎖定狀態,需要網絡的PIN碼解鎖 4
SIM_STATE_READY 就緒狀態 5
*/
int
simStat = telephonyManager.getSimState();
/**
* ICC卡是否存在
*/
boolean
bl= telephonyManager.hasIccCard();
/**
* 是否漫游:
* (在GSM用途下)
*/
boolean
blean = telephonyManager.isNetworkRoaming();
/**
* 附近的電話的信息:
* 類型:List<NeighboringCellInfo>
* 需要權限:android.Manifest.permission#ACCESS_COARSE_UPDATES
*/
List<NeighboringCellInfo>
list =
telephonyManager.getNeighboringCellInfo();//List<NeighboringCellInfo>
/**
* 獲取數據連接狀態
*/
int
dataActivty = telephonyManager.getDataActivity();
}
eg:
目錄結構
main.xml布局文件

<?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"> <ScrollView android:fillViewport="true" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </ScrollView> </LinearLayout>
array.xml文件

<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="listItem"> <item>設備編號</item> <item>SIM卡國別</item> <item>SIM卡序列號</item> <item>SIM卡狀態</item> <item>軟件版本</item> <item>網絡運營商代號</item> <item>網絡運營商名稱</item> <item>手機制式</item> <item>設備當前位置</item> </string-array> <string-array name="simState"> <item>狀態未知</item> <item>無SIM卡</item> <item>被PIN加鎖</item> <item>被PUK加鎖</item> <item>被NetWork PIN加鎖</item> <item>已准備好</item> </string-array> <string-array name="phoneType"> <item>未知</item> <item>GSM</item> <item>CDMA</item> </string-array> </resources>
清單文件

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TelephonyManagerActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> </manifest>
TelephonyManagerActivity類

package com.ljq.activity; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.TelephonyManager; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class TelephonyManagerActivity extends Activity { private ListView listView=null; private TelephonyManager tm=null; private String[] phoneType=null; private String[] simState=null; private String[] listItems=null; ArrayList<String> listValues=new ArrayList<String>(); BaseAdapter adapter=new BaseAdapter(){ public int getCount() { return listItems.length; } public Object getItem(int position) { return listItems[position]; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { LinearLayout ll=new LinearLayout(TelephonyManagerActivity.this); ll.setOrientation(LinearLayout.VERTICAL); TextView tvItem=new TextView(TelephonyManagerActivity.this); tvItem.setTextSize(24); tvItem.setText(listItems[position]); tvItem.setGravity(Gravity.LEFT);//設置在父容器中的對齊方式 ll.addView(tvItem); TextView tvValue=new TextView(TelephonyManagerActivity.this); tvValue.setTextSize(18); //設置字體大小 tvValue.setText(listValues.get(position)); //設置顯示的內容 tvValue.setPadding(0, 0, 10, 10); //設置四周邊界 tvValue.setGravity(Gravity.RIGHT); ll.addView(tvValue); return ll; } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listItems=getResources().getStringArray(R.array.listItem); simState=getResources().getStringArray(R.array.simState); phoneType=getResources().getStringArray(R.array.phoneType); tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); listView=(ListView)findViewById(R.id.listView); initListValues(); listView.setAdapter(adapter); } /** * 獲取各個數據項的值 */ public void initListValues(){ listValues.add(tm.getDeviceId());//獲取設備編號 listValues.add(tm.getSimCountryIso());//獲取SIM卡國別 listValues.add(tm.getSimSerialNumber());//獲取SIM卡序列號 listValues.add(simState[tm.getSimState()]);//獲取SIM卡狀態 listValues.add((tm.getDeviceSoftwareVersion()!=null?tm.getDeviceSoftwareVersion():"未知")); //獲取軟件版本 listValues.add(tm.getNetworkOperator());//獲取網絡運營商代號 listValues.add(tm.getNetworkOperatorName());//獲取網絡運營商名稱 listValues.add(phoneType[tm.getPhoneType()]);//獲取手機制式 listValues.add(tm.getCellLocation().toString());//獲取設備當前位置 } }
運行結果