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