Android获取设备的基本服务信息

TelephonyManager提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。
应用程序也可以注册listener来监听电话状态的改变。
我们不能对TelephonyManager进行实例化,只能通过如下的形式得到对其实例的引用:
Context.getSystemService(Context.TELEPHONY_SERVICE).
注意:对手机的某些信息进行读取是 需要一定许可(permission)的。
可以通过在manifest file中添加一定的许可(permission),以便来对对手机的某些信息进行读取。



先是Java代码
[java]   view plain copy
  1. TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  2.         StringBuilder sb = new StringBuilder();  
  3.         sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());  
  4.         sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());  
  5.         sb.append("\nLine1Number = " + tm.getLine1Number());  
  6.         sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());  
  7.         sb.append("\nNetworkOperator = " + tm.getNetworkOperator());  
  8.         sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());  
  9.         sb.append("\nNetworkType = " + tm.getNetworkType());  
  10.         sb.append("\nPhoneType = " + tm.getPhoneType());  
  11.         sb.append("\nSimCountryIso = " + tm.getSimCountryIso());  
  12.         sb.append("\nSimOperator = " + tm.getSimOperator());  
  13.         sb.append("\nSimOperatorName = " + tm.getSimOperatorName());  
  14.         sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());  
  15.         sb.append("\nSimState = " + tm.getSimState());  
  16.         sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());  
  17.         sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());  
  18.         Log.e("info", sb.toString());   

然后是Manifest.xml中的权限代码
[java]   view plain copy
  1. <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
语音传送的网络类型
int  PHONE_TYPE_CDMA 
Phone radio is CDMA.
int  PHONE_TYPE_GSM 
Phone radio is GSM.
int  PHONE_TYPE_NONE 
No phone radio.
int  PHONE_TYPE_SIP 
Phone is via SIP.
SIM卡状态
int  SIM_STATE_ABSENT 
SIM card state: no SIM card is available in the device
int  SIM_STATE_NETWORK_LOCKED 
SIM card state: Locked: requries a network PIN to unlock
int  SIM_STATE_PIN_REQUIRED 
SIM card state: Locked: requires the user's SIM PIN to unlock
int  SIM_STATE_PUK_REQUIRED 
SIM card state: Locked: requires the user's SIM PUK to unlock
int  SIM_STATE_READY 
SIM card state: Ready
int  SIM_STATE_UNKNOWN 
SIM card state: Unknown.
手机 通话状态广播中的对应值
public static final String  EXTRA_STATE_IDLE 
Value used with EXTRA_STATE corresponding to CALL_STATE_IDLE.
它在手机通话状态改变的广播中,用于表示CALL_STATE_IDLE状态
public static final String  EXTRA_STATE_OFFHOOK 
Value used with EXTRA_STATE corresponding to CALL_STATE_OFFHOOK.
它在手机通话状态改变的广播中,用于表示CALL_STATE_OFFHOOK状态
public static final String  EXTRA_STATE_RINGING 
Value used with EXTRA_STATE corresponding to CALL_STATE_RINGING.
它在手机通话状态改变的广播中,用于表示CALL_STATE_RINGING状态
String  ACTION_PHONE_STATE_CHANGED 
Broadcast intent action indicating that the call state (cellular) on the device has changed.
The EXTRA_STATE extra indicates the new call state. If the new state is RINGING, a second extra EXTRA_INCOMING_NUMBER provides the incoming phone number as a String.
Requires the  READ_PHONE_STATE permission.
This was a sticky broadcast in version 1.0, but it is no longer sticky. Instead, use getCallState() to synchronously query the current call state.
在广播中如果用Action是ACTION_PHONE_STATE_CHANGED来 标示它是 通话状态改变的广播
:需要许可READ_PHONE_STATE。
String  EXTRA_INCOMING_NUMBER  The lookup key used with the ACTION_PHONE_STATE_CHANGED 
broadcast for a String containing the incoming phone number.
在手机通话状态改变的广播,用于从extra取来电号码。
String  EXTRA_STATE  The lookup key used with the ACTION_PHONE_STATE_CHANGED 
broadcast for a String containing the new call state.
在通话状态改变的广播,用于从extra取来通话状态。
主要成员函数:
public int  getCallState()
Returns a constant indicating the call state (cellular) on the device.
取得手机的通话状态。
public CellLocation  getCellLocation ()
Returns the current location of the device. Return null if current location is not available.
返回手机当前所处的位置。如果当前定位服务不可用,则返回null
注:需要许可(Permission)ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int  getDataActivity ()
Returns a constant indicating the type of activity on a data connection (cellular).
返回当前数据连接活动状态的情况。
public int  getDataState ()
Returns a constant indicating the current data connection state (cellular).
返回当前数据连接状态的情况。
public String  getDeviceId ()
Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available.
返回手机的设备ID。比如对于GSM的手机来说是IMEI码,对于CDMA的手机来说MEID码或ESN码。如果读取失败,则返回null。
:需要许可(Permission)READ_PHONE_STATE
:手机上的IMEI就是手机身份识别号码。IMEI码贴在手机背面的标志上,并且读写于手机内存中。它也是该手机在厂家的"档案"和"身份证号"。
关于IMEI的更多内容参考《手机身份识别码之IMEI》
public String  getDeviceSoftwareVersion ()
Returns the software version number for the device, for example, the IMEI/SV for GSM phones. Return null if the software version is not available.
返回手机软件版本号。比如对于GSM手机返回的是IMEI/SV码。如果读取失败,则返回null。
注1:需要许可(Permission)READ_PHONE_STATE
注2:按照文档该函数返回的还是IMEI和getDeviceId ()一样。不解。
String  getLine1Number ()
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
返回手机的Line1Number。比如对于GSM手机返回的是MSISDN码。如果读取失败,则返回null。
注1:需要许可(Permission)READ_PHONE_STATE
注2:关于MSISDN的更多内容请参考《MSISDN(唯一能识别移动用户的号码)》
public List<NeighboringCellInfo>  getNeighboringCellInfo ()
Returns the neighboring cell information of the device.
Returns
    * List of NeighboringCellInfo or null if info unavailable.
返回手机的neighboring cell信息
      :文档上说需要许可(Permission)ACCESS_COARSE_UPDATES,但是在android.Manifest.permission中却找不到该常量。
public String  getNetworkCountryIso ()
Returns the ISO country code equivalent of the current registered operator's MCC (Mobile Country Code).
返回网络所在的国家代码(ISO标准形式)
注1: 只有手机注册到了网络,该功能才可用. 
注2:如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
public String  getNetworkOperator ()
Returns the numeric name (MCC+MNC) of current registered operator.
返回当前网络提供商的数字名字(MCC+MNC的形式)
如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
:MCC(Mobile Country Code,移动国家号码),用于唯一地标识移动客户属于的国家。它由三位十进制数组成(000-999),例如我国的MCC为460
public String  getNetworkOperatorName ()
Returns the alphabetic name of current registered operator.
返回当前网络提供商的名字(字母形式)
如果是在CDMA的网络中,返回的结果可能不可靠(可通过getPhoneType()来判断是否在CDMA网络中)。
public int  getNetworkType ()
Returns a constant indicating the radio technology (network type) currently in use on the device for data transmission.
返回用于传输数据的网络的无线类型。比如GPRS
public int  getPhoneType ()
Returns a constant indicating the device phone type. This indicates the type of radio used to transmit voice calls.
返回手机用于传输语言的无线类型。比如GSM,CDMA
public String  getSimCountryIso ()
Returns the ISO country code equivalent for the SIM provider's country code.
返回SIM卡提供商的国家代码。
public String getSimOperator ()
Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM. 5 or 6 decimal digits.
返回SIM卡的提供商代码,以MCC+MNC的形式(mobile country code + mobile network code)。该码是5或6为的十进制形式。
:只有SIM的状态是SIM_STATE_READY的时候,该功能才可用。
public String  getSimOperatorName ()
Returns the Service Provider Name (SPN).
返回SIM卡服务商的名字。
注:只有SIM的状态是SIM_STATE_READY的时候,该功能才可用。
public String  getSimSerialNumber ()
Returns the serial number of the SIM, if applicable. Return null if it is unavailable.
返回SIM卡的编码。如果读取失败,则返回null。
注:需要许可(Permission)READ_PHONE_STATE
public int  getSimState ()
Returns a constant indicating the state of the device SIM card.
返回SIM卡的状态
public String  getSubscriberId ()
Returns the unique subscriber ID, for example, the IMSI for a GSM phone. Return null if it is unavailable.
返回国际移动用户识别码。对于GSM手机来说是IMSI。如果读取失败,则返回null。
注1:imsi是国际移动用户识别码。它主要是用于国际漫游的。这个对用户来说应该是透明的。关于imsi的更多内容可参考《国际移动用户识别码imsi》
注2:需要许可(Permission)READ_PHONE_STATE
public String  getVoiceMailAlphaTag ()
Retrieves the alphabetic identifier associated with the voice mail number.
使用语言邮箱号得到其字母形式的标识符(和语音邮件相关的标签)
注:需要许可(Permission)READ_PHONE_STATE
public String  getVoiceMailNumber ()
Returns the voice mail number. Return null if it is unavailable.
得到语音邮箱码。
:需要许可(Permission)READ_PHONE_STATE
public boolean  hasIccCard ()
Returns
    * true if a ICC card is present 
    ICC卡是否存在
public boolean  isNetworkRoaming ()
Returns true if the device is considered roaming on the current network, for GSM purposes.
查看是否在漫游。
:它只在GSM下使用。
public void  listen (PhoneStateListener listener, int events)
Registers a listener object to receive notification of changes in specified telephony states.
To register a listener, pass a PhoneStateListener and specify at least one telephony state of interest in the events argument. At registration, and when a specified telephony state changes, the telephony manager invokes the appropriate callback method on the listener object and passes the current (udpated) values.
To unregister a listener, pass the listener object and set the events argument to LISTEN_NONE (0).
Parameters
listener  The PhoneStateListener object to register (or unregister)
events  The telephony state(s) of interest to the listener, as a bitwise-OR combination of PhoneStateListener LISTEN_ flags. 
可以通过PhoneStateListener listener来监听手机的状态。
1:unregister是通过把events设置为LISTEN_NONE (0)来实现的。
2:PhoneStateListener除了可以监听手机的通话状态,数据连接活动状态,数据连接状态还可以监听很多状态。
具体见《PhoneStateListener》
3:手机的通话状态可以通过Receiver来监听,具体见上面关于ACTION_PHONE_STATE_CHANGED的说明.

工程资源: http://download.csdn.net/detail/etzmico/3839135
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值