public class
TelephonyManager
extends Object
java.lang.Object
android.telephony.TelephonyManager
TelephonyManager提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。
应用程序也可以注册listener来监听电话状态的改变。
我们不能对TelephonyManager进行实例化,只能通过如下的形式得到对其实例的引用:
Context.getSystemService(Context.TELEPHONY_SERVICE);
注意:对手机的某些信息进行读取是需要一定许可(permission)的。
可以通过在manifest file中添加一定的许可(permission),以便来对对手机的某些信息进行读取。
主要静态成员常量
手机的通话状态
它们对应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_PHONE_STATE_CHANGED这个Action来标示通话状态改变的广播(intent)。
注:需要许可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码贴在手机背面的标志上,并且读写于手机内存中。它也是该手机在厂家的"档案"和"身份证号"。
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
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
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是国际移动用户识别码。它主要是用于国际漫游的。这个对用户来说应该是透明的。
注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).
参数
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.LISTEN_CALL_STATE等的或运算值。
可以通过PhoneStateListener listener来监听手机的状态。
注1:unregister是通过把events设置为LISTEN_NONE (0)来实现的。
注2:PhoneStateListener除了可以监听手机的通话状态,数据连接活动状态,数据连接状态还可以监听很多状态。
注3:手机的通话状态可以通过Receiver来监听,具体见上面关于ACTION_PHONE_STATE_CHANGED的说明.
使用PhoneStateListener能监听以下它的静态常量标示的所有内容
PhoneStateListener的监听接口如下
通过向TelephonyManager注册一个listener,就可以监听手机的一些状态的变化。
自定义的监听器:
class MyPhoneStateListener extends PhoneStateListener
{
Context context;
public MyPhoneStateListener(Context con)
{
context = con;
}
public void onCallForwardingIndicatorChanged(boolean cfi)
{
}
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "call not answer", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "incoming", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "in a call", Toast.LENGTH_LONG).show();
break;
}
}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
}
注册自己的监听器:
String srvcName = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(srvcName);
MyPhoneStateListener phoneStateListener = new MyPhoneStateListener(this);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
转自:http://blog.csdn.net/skiffloveblue/article/details/7491618
private PhoneStateManager mPhoneStateManager;
mPhoneStateManager = new PhoneStateManager(this, mCallStateListener);
if (mPhoneStateManager != null) {
mPhoneStateManager.stopListen();
}
private CallStateListener mCallStateListener = new CallStateListener() {
@Override
public void onCallStateChange(int state, String incomingNumber) {
// TODO Auto-generated method stub
mCurrentModule.onCallStateChanged(state, incomingNumber);
if (mPhoneStateManager.getPhoneState() == TelephonyManager.CALL_STATE_IDLE && mPhoneAlertDialog != null &&
mPhoneAlertDialog.isShowing()) {
mPhoneAlertDialog.dismiss();
}
if (state != TelephonyManager.CALL_STATE_IDLE) {
isPhoneCall = true;
}
}
};
- Android之TelephonyManager类的方法详解
TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取 ...
- Android之TelephonyManager类的使用案例
TelephonyManager类主要提供了一系列用于访问与手机通讯相关的状态和信息的get方法.其中包括手机SIM的状态和信息.电信网络的状态及手机用户的信息.在应用程序中可以使用这些get方法获取 ...
- Android TelephonyManager类的使用
TelephonyManager类主要提供了一系列获取手机与通讯相关的状态和信息的get方法,包含手机用户的信息.手机SIM的状态.电信网络的状态等. TelephonyManager类的对象的获取: ...
- TelephonyManager类:Android手机及Sim卡状态的获取
TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app;import android.ap ...
- android系统中使用TelephonyManager类来获取imei号和其他手机信息
在AndroidManifest.xml文件中增加<!--允许读取电话状态SIM的权限--><uses-permission android:name="android.p ...
- TelephonyManager&GsmCellLocation类的方法详解
转载:http://blog.163.com/zhangzheming_282/blog/static/117920962011101944356511/ TelephonyManager类 主要提供 ...
- Android提供的系统服务之--TelephonyManager(电话管理器)
Android提供的系统服务之--TelephonyManager(电话管理器) 转载请注明出处--coder-pig TelephonyManager的作用: 用于管理手机通话状态,获取电话信息(设 ...
- Android之TelephonyManager
在Android平台中,通过TelephonyManager可以访问与手机通讯相关的信息,比如设备信息.网络信息及SIM卡信息,同时还可以监听电话的相关状态.下面我们通过几个方面来说明Android平 ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
随机推荐
- C#复习②
C#复习② 2016年6月15日 09:08 1.C#之Symbols Identifier = (letter|'_'|'@'){letter|digit|'_'}. 需要注意: 1.Unicode ...
- Android简单加密保护自有图片资源
现在大部分android应用的图片资源,被反编译后就可以直接拿来用,如果不想让自己的图片资源直接被反编译后使用,首先想到的应该是把图片加密.这里笔者抛砖引玉,草草写了一个对图片进行简单加密的方法,希望 ...
- 记录一些在用wcf的过程中走过的泥巴路 【第一篇】
自从转移战场之后,比以前忙多了,博客也没能及时跟上,原本准备继续mvc系列,但是在那边技术比较陈旧还没能用得上,话说有3年没接触这玩意了,东西也 都忘了差不多了,既然再次接触,我也就继续温习温习,记录 ...
- lamp安装
一.简介 什么是LAMPLAMP是一种Web网络应用和开发环境,是Linux, Apache, MySQL, Php/Perl的缩写,每一个字母代表了一个组件,每个组件就其本身而言都是在它所代表的方面 ...
- mac 安装 php nginx mysql
mac下面安装php nginx mysql根linux下面差不多,建议大家使用brew管理工具包安装. 1,安装homebrew http://brew.sh/index_zh-cn.html 安装 ...
- 烂泥:apache密码生成工具htpasswd的应用
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 1. htpasswd的作用与安装 2. htpasswd命令详解 3. htpasswd的实例 4. htpasswd的应用 一.htpasswd的作 ...
- Android,LIstView中的OnItemClick点击无效的解决办法
在List_Item布局文件中的根节点加上如下背景标黄的这一行 <?xml version="1.0" encoding="utf-8"?> < ...
- 在Python命令行和VIM中自动补全
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1. VIM下的配置: wget https://github.com/rkulla/pydiction/arc ...
- 今天有事-MySQL
hi 今天有事,一会儿要去耍,能学多少是多少吧 1.MySQL -----子查询与连接(二)----- ----子查询 子查询,是指出现在其他SQL语句内的SELECT子句 注意:子查询指嵌套在查询内 ...
- Java方法区和运行时常量池溢出问题分析
运行时常量池是方法区的一部分,方法区用于存放Class的相关信息,如类名.访问修饰符.常量池.字段描述.方法描述等. String.intern()是一个native方法,它的作用是:如果字符串常量池 ...