• TelephonyManager类与PhoneStateListener


    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码贴在手机背面的标志上,并且读写于手机内存中。它也是该手机在厂家的"档案"和"身份证号"。
    关于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
    注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).
    参数
    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的监听接口如下
    Public Methods
    void onCallForwardingIndicatorChanged(boolean cfi)
    Callback invoked when the call-forwarding indicator changes.
    void onCallStateChanged(int state, String incomingNumber)
    Callback invoked when device call state changes.
    void onCellLocationChanged(CellLocation location)
    Callback invoked when device cell location changes.
    void onDataActivity(int direction)
    Callback invoked when data activity state changes.
    void onDataConnectionStateChanged(int state)
    Callback invoked when connection state changes.
    void onDataConnectionStateChanged(int state, int networkType)
    same as above, but with the network type.
    void onMessageWaitingIndicatorChanged(boolean mwi)
    Callback invoked when the message-waiting indicator changes.
    void onServiceStateChanged(ServiceState serviceState)
    Callback invoked when device service state changes.
    void onSignalStrengthChanged(int asu)
    This method is deprecated. Use onSignalStrengthsChanged(SignalStrength)
    void onSignalStrengthsChanged(SignalStrength signalStrength)
    Callback invoked when network signal strengths changes.
     
     

    通过向TelephonyManager注册一个listener,就可以监听手机的一些状态的变化。

    自定义的监听器:

     1 class MyPhoneStateListener extends PhoneStateListener      
     2 {      
     3     Context context;      
     4     public MyPhoneStateListener(Context con)      
     5     {      
     6          context = con;      
     7     }      
     8           
     9     public void onCallForwardingIndicatorChanged(boolean cfi)       
    10     {      
    11               
    12     }      
    13           
    14     public void onCallStateChanged(int state, String incomingNumber)       
    15     {      
    16         switch(state)      
    17         {      
    18         case TelephonyManager.CALL_STATE_IDLE:      
    19             Toast.makeText(context, "call not answer", Toast.LENGTH_LONG).show();      
    20             break;      
    21                   
    22         case TelephonyManager.CALL_STATE_RINGING:      
    23             Toast.makeText(context, "incoming", Toast.LENGTH_LONG).show();      
    24                   
    25             break;      
    26                   
    27         case TelephonyManager.CALL_STATE_OFFHOOK:      
    28             Toast.makeText(context, "in a call", Toast.LENGTH_LONG).show();      
    29             break;      
    30         }      
    31     }      
    32           
    33     public void onCellLocationChanged(CellLocation location) {}      
    34           
    35     public void onDataActivity(int direction) {}      
    36           
    37     public void onDataConnectionStateChanged(int state) {}      
    38           
    39     public void onMessageWaitingIndicatorChanged(boolean mwi) {}      
    40           
    41     public void onServiceStateChanged(ServiceState serviceState) {}      
    42           
    43     public void onSignalStrengthChanged(int asu) {}      
    44 }    

    注册自己的监听器:

     1 String srvcName = Context.TELEPHONY_SERVICE;    
     2         TelephonyManager telephonyManager = (TelephonyManager)getSystemService(srvcName);    
     3             
     4         MyPhoneStateListener phoneStateListener = new MyPhoneStateListener(this);    
     5         telephonyManager.listen(phoneStateListener,    
     6                 PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |    
     7                 PhoneStateListener.LISTEN_CALL_STATE |    
     8                 PhoneStateListener.LISTEN_CELL_LOCATION |    
     9                 PhoneStateListener.LISTEN_DATA_ACTIVITY |    
    10                 PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |    
    11                 PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |    
    12                 PhoneStateListener.LISTEN_SERVICE_STATE |    
    13                 PhoneStateListener.LISTEN_SIGNAL_STRENGTH);    

    转自:http://blog.csdn.net/skiffloveblue/article/details/7491618

     1 private PhoneStateManager mPhoneStateManager;
     2 
     3 mPhoneStateManager = new PhoneStateManager(this, mCallStateListener);
     4 
     5 if (mPhoneStateManager != null) {
     6             mPhoneStateManager.stopListen();
     7         }
     8 
     9 private CallStateListener mCallStateListener = new CallStateListener() {
    10 
    11         @Override
    12         public void onCallStateChange(int state, String incomingNumber) {
    13             // TODO Auto-generated method stub
    14             mCurrentModule.onCallStateChanged(state, incomingNumber);
    15             if (mPhoneStateManager.getPhoneState() == TelephonyManager.CALL_STATE_IDLE && mPhoneAlertDialog != null &&
    16                     mPhoneAlertDialog.isShowing()) {
    17                 mPhoneAlertDialog.dismiss();
    18             }
    19             if (state != TelephonyManager.CALL_STATE_IDLE) {
    20                 isPhoneCall = true;
    21             }
    22         }
    23     };
  • 相关阅读:
    linq查询结果指定列的两种方式
    MVC HTML辅助类常用方法记录
    如何获取google地图、baidu百度地图的坐标
    js解析Json字符串的方法
    EF 官方API
    sqlserver 计算 百分比
    js 判断js函数、变量是否存在
    JS 去字符串空格 总结
    sql 转换日期格式 只保留月份和日期
    C# json object互转工具
  • 原文地址:https://www.cnblogs.com/zl1991/p/5566761.html
Copyright © 2020-2023  润新知