addAction到IntentFilter来处理电话状态更改

时间:2017-03-06 02:59:31

标签: android broadcastreceiver telephonymanager android-broadcastreceiver phone-state-listener

我想听一个来电。

我知道您必须在AndroidManifest中提供权限并使用

设置接收器
<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

<receiver ... 
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />...


但是,在我的项目中,我有按代码设置接收器,

IntentFilter filter = new IntentFilter();
filter.addAction( ... );
registerReceiver(broadcastReceiver, filter);

addActionTelephonyManager.ACTION_PHONE_STATE_CHANGED private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ...
我用

创建BroadcastReceiver
String action = intent.getAction();

然后,在其中,我从filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 的意图中采取行动,并检查它是什么动作。

它非常适合蓝牙

switch (action) {
    case BluetoothAdapter.ACTION_DISCOVERY_STARTED: ...

TelephonyManager.ACTION_PHONE_STATE_CHANGED

但它不会检查addAction()

所以,我的问题是:
如何设置动作(TelephonyManager.ACTION_PHONE_STATE_CHANGED)来听取状态变化?
BluetoothDevice.ACTION_FOUND BluetoothAdapter.ACTION_DISCOVERY_STARTED BluetoothAdapter.ACTION_DISCOVERY_FINISHED 不对吗?
如果我放弃并在AndroiManifest中设置接收器,我该如何设置蓝牙滤波器?我正在使用

/*Desktop Query*/
@media only screen and (min-width: 768px) {
  body {
    background: black;
  }
}

/*Mobile Query*/
@media only screen and (max-width: 480px) {
  body {
    background-color: tomato;
  }
}

/*Tablet Query*/
@media only screen and (min-width: 481px) and (max-width:768px) {
  body {
    background: pink;
  }
}

2 个答案:

答案 0 :(得分:1)

在您的活动中首先注册接收者

this.registerReceiver(this.broadcastReceiver, new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED));

这是broadcastReceiver:

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state == null) {

            //Outgoing call
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.e("tag", "Outgoing number : " + number);

        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            Log.e("tag", "EXTRA_STATE_OFFHOOK");

        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

            Log.e("tag", "EXTRA_STATE_IDLE");

        } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

            //Incoming call
            String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Log.e("tag", "Incoming number : " + number);

        } else
            Log.e("tag", "none");
    }
};

答案 1 :(得分:0)

使用onCreate()方法创建此对象:

tMg =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

listener = new PhoneStateListener{

    onCallStateChanged(int state, String incomingNumber){
         switch(state){
               case EXTRA_STATE_RINGING:
                   // phone is ringing
               break;
               case EXTRA_STATE_OFFHOOK:
                  // phone is offhook
               break;
               case EXTRA_STATE_IDLE:
                 // phone is idle
               break;
         }
    }

}

onStart()注册你的听众:

tMg.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

onStop()取消注册您的听众:

tMg.listen(listener,  PhoneStateListener.LISTEN_NONE);