即使应用程序关闭,我也想使用广播接收器

时间:2015-06-21 17:59:07

标签: android

我正在尝试在接听来电/拨出电话时显示Toast留言。 如果应用关闭,接收器将无法工作。 我不想使用服务。请帮帮我。

    'I am using the below receiver code'
    public class CallReceiver extends BroadcastReceiver 
    {
        @Override
        public void onReceive(Context context, Intent intent) {
                if (isConnected(context)) {
                    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                         Toast.makeText(context, "Call in progress", Toast.LENGTH_LONG).show();
                    }
                }
            }

    'This is receiver registered in manifest'
    <receiver android:name="com.example.android.testapplication.CallReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"></action>
            <action android:name="android.intent.action.new_outgoing_call"></action>
        </intent-filter>
    </receiver>

2 个答案:

答案 0 :(得分:0)

尝试将手机状态权限添加到您的清单。

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

答案 1 :(得分:0)

默认情况下,当BroadCastReceiver注册AndroidOS时,Receiver始终作为Service部分工作,即使您的应用程序无效,因为您不必担心这个问题。 我认为问题在于你注册的方式Receiver是不正确的。

使用in / out调用你应该使用PhoneStateListener,它覆盖了onCallStateChanged方法。你可以在那里使用3个州。

也许this example会有所帮助。