TelephonyManager提供了取得手機基本服務的信息的一種方式。因此應用程序可以使用TelephonyManager來探測手機基本服務的情況。
應用程序也可以注冊listener來監聽電話狀態的改變。
我們不能對TelephonyManager進行實例化,只能通過如下的形式得到對其實例的引用:
Context.getSystemService(Context.TELEPHONY_SERVICE).
注意:對手機的某些信息進行讀取是
需要一定許可(permission)的。
可以通過在manifest file中添加一定的許可(permission),以便來對對手機的某些信息進行讀取。
本文源碼下載:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android教程:獲取設備的基本服務信息/
先是Java代碼
- TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- StringBuilder sb = new StringBuilder();
- sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
- sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
- sb.append("\nLine1Number = " + tm.getLine1Number());
- sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());
- sb.append("\nNetworkOperator = " + tm.getNetworkOperator());
- sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());
- sb.append("\nNetworkType = " + tm.getNetworkType());
- sb.append("\nPhoneType = " + tm.getPhoneType());
- sb.append("\nSimCountryIso = " + tm.getSimCountryIso());
- sb.append("\nSimOperator = " + tm.getSimOperator());
- sb.append("\nSimOperatorName = " + tm.getSimOperatorName());
- sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());
- sb.append("\nSimState = " + tm.getSimState());
- sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());
- sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
- Log.e("info", sb.toString());
然後是Manifest.xml中的權限代碼
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
我們把獲得的內容信息展示到屏幕上,方便閱覽。
由於我在家中是用的虛擬機,因此一些信息無法正常顯示,先呈上一個虛擬機運行的效果圖,實機的會過幾天補上。
主要靜態成員常量:
手機的
通話狀態:(它們對應PhoneStateListener.
LISTEN_CALL_STATE)
int
CALL_STATE_IDLE Device call state: No activity.
Device call state: No activity.
空閒。沒有任何活動。
int
CALL_STATE_OFFHOOK Device call state: Off-hook.
Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
摘機狀態。至少有個電話活動。該活動或是撥打(dialing)或是通話,或是 on hold。並且沒有電話是ringing or waiting
注:on hold的意思是暫時掛起。比如在進行通話A的時候,來了個新的電話B,我們暫時掛起通話A,等通話B結束了,才繼續進行A的通話。
int
CALL_STATE_RINGING Device call state: Ringing.
Device call state: Ringing. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
來電。來電,電話鈴聲響起的那段時間或正在通話又來新電,新來電話不得不等待的那段時間。
數據連接活動狀態(它們對應PhoneStateListener.
LISTEN_DATA_ACTIVITY)
int
DATA_ACTIVITY_DORMANT
Data connection is active, but physical link is down
int
DATA_ACTIVITY_IN
Data connection activity: Currently receiving IP PPP traffic.
int
DATA_ACTIVITY_INOUT
Data connection activity: Currently both sending and receiving IP PPP traffic.
int
DATA_ACTIVITY_NONE
Data connection activity: No traffic.
int
DATA_ACTIVITY_OUT
Data connection activity: Currently sending IP PPP traffic.
數據連接狀態(它們對應PhoneStateListener.
LISTEN_DATA_CONNECTION_STATE )
int
DATA_CONNECTED
Data connection state: Connected.
int
DATA_CONNECTING
Data connection state: Currently setting up a data connection.
int
DATA_DISCONNECTED
Data connection state: Disconnected.
int
DATA_SUSPENDED
Data connection state: Suspended.
數據傳送的網絡類型
int
NETWORK_TYPE_1xRTT
Current network is 1xRTT
int
NETWORK_TYPE_CDMA
Current network is CDMA: Either IS95A or IS95B
int
NETWORK_TYPE_EDGE
Current network is EDGE
int
NETWORK_TYPE_EHRPD
Current network is eHRPD
int
NETWORK_TYPE_EVDO_0
Current network is EVDO revision 0
int
NETWORK_TYPE_EVDO_A
Current network is EVDO revision A
int
NETWORK_TYPE_EVDO_B
Current network is EVDO revision B
int
NETWORK_TYPE_GPRS
Current network is GPRS
int
NETWORK_TYPE_HSDPA
Current network is HSDPA
int
NETWORK_TYPE_HSPA
Current network is HSPA
int
NETWORK_TYPE_HSUPA
Current network is HSUPA
int
NETWORK_TYPE_IDEN
Current network is iDen
int
NETWORK_TYPE_LTE
Current network is LTE
int
NETWORK_TYPE_UMTS
Current network is UMTS
int
NETWORK_TYPE_UNKNOWN
Network type is unknown