当呼叫进入和用户拨打号码时需要关闭服务

时间:2011-05-07 22:23:23

标签: android android-intent android-context telephony

所以,我有一个在后台运行的服务(这是超级秘密,所以我不能告诉你它是什么:))但我需要能够在用户发起呼叫或呼叫即将到来时将其关闭到目前为止,我已经检查了Intent.ACTION_ANSWER,但由于某种原因,我的广播接收器从未检测到它。我也尝试使用PhoneStateListener,但在我的案例陈述中,我无法理解如何对我的服务做任何事情。 PhoneStateListener的上下文是什么?

我的BroadcastReceiver的一些示例代码:

public void onReceive(Context context, Intent intent) {     
if (intent.getAction().equals(Intent.ACTION_ANSWER)) {
        phoneRinging = true;            
    }

Intent i = new Intent(context, MyService.class);        
    i.putExtra("phone", phoneRinging);
    context.startService(i);
}

以下是通过PhoneStateListener启动服务的代码段。 示例:startService(new Intent(---CONTEXT---, MySuperSecretService.class))

接收器在清单中:

<receiver android:name=".PhoneReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.ANSWER"></action>
    </intent-filter>
</receiver>

如果我在CONTEXT中,WTH会进入此声明的PhoneStateListener部分吗?

1 个答案:

答案 0 :(得分:0)

请检查ACTION_PHONE_STATE_CHANGED而不是ACTION_ANSWER。 ACTION_ANSWER不是来电的广播意图。这是一项标准的活动行动。