电话状态监听小结

来源:互联网 发布:通过ip添加网络打印机 编辑:程序博客网 时间:2024/05/17 04:09

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                          
receiver:

private BroadcastReceiver mPhoneStateReceiver = new BroadcastReceiver() {          @Override      public void onReceive(Context context, Intent intent) {                      TelephonyManager tm =                           (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                                                LogUtil.d(TAG, "phone state: " + tm.getCallState());                    switch (tm.getCallState()) {                       case TelephonyManager.CALL_STATE_IDLE:                       if(mLossOfFocusDuringGainDelay == true && mHasAudioFocusWhenCall){                               int delay = 0;                               mLossOfFocusDuringGainDelay = false;                               mMediaplayerHandler.sendMessageDelayed(                                       mMediaplayerHandler.obtainMessage(FOCUSCHANGE, AudioManager.AUDIOFOCUS_GAIN, 0), delay);                       }                       mHasAudioFocusWhenCall = false;                       break;                    }       }  } ;


注册:(记得在unresgister)

 private void registerPhoneStateReceiver(){    registerReceiver(mPhoneStateReceiver, new IntentFilter("android.intent.action.PHONE_STATE"));    }

还有一个permission:

//状态阅读和去电准许

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />



来电的三种状态:

/** Device call state: No activity. */    public static final int CALL_STATE_IDLE = 0;    /** Device call state: Ringing. A new call arrived and is     *  ringing or waiting. In the latter case, another call is     *  already active. */    public static final int CALL_STATE_RINGING = 1;    /** Device call state: Off-hook. At least one call exists      * that is dialing, active, or on hold, and no calls are ringing      * or waiting. */    public static final int CALL_STATE_OFFHOOK = 2;
CALL_STATE_IDLE:空闲状态,就是没有挂掉电话的状态

CALL_STATE_RINGING:响铃状态

CALL_STATE_OFFHOOK:通话状态。就是已经再对话了。



去电广播:

Intent.ACTION_NEW_OUTGOING_CALL

具体的状态不知道,也找不到,可能和来电的state共用吧。


其他相关微博:

http://www.jb51.net/article/32433.htm





0 0
原创粉丝点击