android-PhoneStateListener

honeStateListener
1.对特定的电话状态的监听,包括服务的状态、信号强度、消息等待指示(语音信箱)、通话转移、呼叫状态、设备单元位置、数据连接状态、数据流量方向。一些电话信息受权限保护,应用程序不会收到受保护的信息的更新,除非在manifest文件中有适当的权限声明。凡申请许可,有适当的LISTEN_标志。

2.对监听的话做处理

Handler mHandler = new Handler() {
   public void handleMessage(Message msg) {
       switch (msg.what) {
           case LISTEN_SERVICE_STATE:/*Listen for changes to the network service state (cellular).对网络服务状态监听*/
              PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj);
              break;
           case LISTEN_SIGNAL_STRENGTH:/*Listen for changes to the network signal strength (cellular).对网络信号强度变化监听*/
              PhoneStateListener.this.onSignalStrengthChanged(msg.arg1);
              break;
           case LISTEN_MESSAGE_WAITING_INDICATOR:/*Listen for changes to the message-waiting indicator.对消息等待指示的变化监听*/
              PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0);
              break;
           case LISTEN_CALL_FORWARDING_INDICATOR:/*Listen for changes to the call-forwarding indicator.对通话转移指示的变化监听*/
              PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0);
              break;
           case LISTEN_CELL_LOCATION:/*Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener.对设备单元位置的变化监听,这会导致频繁的监听回调。*/
              PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj);
              break;
           case LISTEN_CALL_STATE:/*Listen for changes to the device call state.对设备呼叫状态的变化监听。*/
              PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj);
              break;
           case LISTEN_DATA_CONNECTION_STATE:/*Listen for changes to the data connection state (cellular).对数据连接状态的变化监听。*/
              PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2);
              PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1);
              break;
           case LISTEN_DATA_ACTIVITY:/*Listen for changes to the direction of data traffic on the data connection (cellular).对数据流量移动方向的变化监听*/
            PhoneStateListener.this.onDataActivity(msg.arg1);
              break;
           case LISTEN_SIGNAL_STRENGTHS:/*Listen for changes to the network signal strengths (cellular).对网络信号强度的变化监听*/
              PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj);
              break;
      }
   }
};


3.监听变化后发送消息

IPhoneStateListener callback = new IPhoneStateListener.Stub() {
    public void onServiceStateChanged(ServiceState serviceState) {
        Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget();
    }
    public void onSignalStrengthChanged(int asu) {
        Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget();
    }
    public void onMessageWaitingIndicatorChanged(boolean mwi) {
        Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null)
                .sendToTarget();
    }
    public void onCallForwardingIndicatorChanged(boolean cfi) {
        Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null)
                .sendToTarget();
    }
    public void onCellLocationChanged(Bundle bundle) {
        CellLocation location = CellLocation.newFromBundle(bundle);
        Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget();
    }
    public void onCallStateChanged(int state, String incomingNumber) {
        Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget();
    }
    public void onDataConnectionStateChanged(int state, int networkType) {
        Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType, null).
                sendToTarget();
    }
    public void onDataActivity(int direction) {
        Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget();
    }
    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
        Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget();
    }
};


Android 平台上,可以通过以下方式判断当前是否处于通话状态: 1. 使用 TelephonyManager 类的 getCallState() 方法获取当前电话的呼叫状态。该方法返回值为 int 类型,表示电话状态的各种情况,如下所示: TelephonyManager.CALL_STATE_IDLE:电话空闲状态,没有通话和等待。 TelephonyManager.CALL_STATE_RINGING:电话响铃状态,等待来电接听。 TelephonyManager.CALL_STATE_OFFHOOK:电话接通状态,正在通话中。 2. 在 Android 9.0 及以上版本中,还可以使用 TelecomManager 类的 addListener() 方法注册一个通话状态监听器,以便实时获取通话状态的变化情况。该方法需要传入一个 CallStateListener 对象,该对象实现了 onCallStateChanged() 方法,用于处理通话状态的变化事件。 以下是一个简单的示例代码: ```java public class MainActivity extends AppCompatActivity { private TelephonyManager mTelephonyManager; private CallStateListener mCallStateListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mCallStateListener = new CallStateListener(); mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_CALL_STATE); } @Override protected void onDestroy() { super.onDestroy(); mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_NONE); } private class CallStateListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String phoneNumber) { super.onCallStateChanged(state, phoneNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: Log.d("MainActivity", "电话空闲"); break; case TelephonyManager.CALL_STATE_RINGING: Log.d("MainActivity", "电话响铃"); break; case TelephonyManager.CALL_STATE_OFFHOOK: Log.d("MainActivity", "电话接通"); break; } } } } ``` 注意:为了保证应用程序的性能和电量效率,建议在不需要监听通话状态时,调用 TelephonyManager 的 listen() 方法,将监听器从系统中注销。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值