目前,我有收听呼叫状态事件的广播接收器。我已经在AndroidManifest.xml注册了广播接收器,如下所示。
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>当应用程序启动时,这个广播接收器被注册为侦听、呼叫、状态、事件,并且根据CALL_STATE,我正在管理我的应用程序。
在手机重新启动之前,它还可以正常工作。电话重新启动后,此广播接收器停止工作。我知道我必须注册接收者来收听系统的BOOT_COMPLETED事件。
我所做的如下:
<receiver android:name=".api.PhoneCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>我还允许获得BOOT_COMPLETED系统事件。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />但不知何故,这是行不通的。我正在考虑制作新的广播接收器,只听BOOT_COMPLETED事件,但问题是
因此,我的问题是,当任何来电进入时,我如何启动这个电话听众广播接收器?
如何从另一个广播接收器注册广播接收器?
是否必须将现有广播接收器的代码移动到服务中,以便从启动接收器启动服务?
任何帮助都将不胜感激。
发布于 2016-06-13 11:45:18
欢迎任何其他答案。
我已经通过创建新的广播接收器来解决这个问题,当电话重新启动时,将调用广播接收器的onReceive()方法,然后动态注册READ_PHONE_STATE广播接收器,这也是显式注册接收机。
以下是代码:
AndroidManifest.xml:
<receiver android:name=".api.ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>BroadcastReceiver:
public class ServiceStarter extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
PhoneCallReceiver receiver = new PhoneCallReceiver();
context.getApplicationContext().registerReceiver(receiver, filter);
}
}您必须使用应用程序上下文注册接收方,如下所示:
context.getApplicationContext().registerReceiver(receiver, filter);而不是
context.registerReceiver(receiver, filter);否则,您将得到以下异常:
android.content.ReceiverCallNotAllowedException: java.lang.RuntimeException:无法启动接收方com.ecosmob.contactpro.api.ServiceStarter: com.ecosmob.contactpro.api.ServiceStarter BroadcastReceiver组件不允许注册接收意图
我希望它能帮到别人!
https://stackoverflow.com/questions/37787291
复制相似问题