1

我需要创建一个BroadcastReceiver监听电话何时接听或拨打电话的功能,这样我就可以记下它何时开始和何时结束。我意识到这两个接收器看起来几乎相同,因此BroadcastReceiver我可以为传入和传出呼叫创建两个单独的 s,而不是为两者创建一个,并使我的操作取决于触发的事件。

我在清单中注册intent-filter了 s android.intent.action.PHONE_STATEandroid.intent.action.NEW_OUTGOING_CALL但是我如何从被解雇onReceive()的类型中找出?Intent

@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();

    // get the intent fired--incoming or outgoing call?
    // then, save it in a variable and perform corresponding actions
}
4

3 回答 3

7

只需使用 intent.getAction();

String action=intent.getAction();

if(action.equalsIgnoreCase(Intent.ACTION_NEW_OUTGOING_CALL)){

//dosomething here
}
else if(action.equalsIgnoreCase(second action)){

//do something here
}
于 2012-09-05T07:24:52.700 回答
2

将您的操作与intent.getAction()using进行比较equals()

于 2012-09-05T07:23:31.803 回答
0

You can also do one thing is to put one string parameter to bundle at the time of setting pending intent, that makes possible to know from which intent OnReceive calls.Also can use " intent.getAction() " Hope it helps :)

于 2012-09-05T07:28:02.450 回答