我为我的Android应用程序实现了很少的服务。我的其中一项服务A_Service
会检查B_Service
是否已在运行,如果不是,它会通过sendBroadcast()
发送广播以到达B_Broadcast_Receiver
的广播接收方B_Service
。< / p>
B_Broadcast_Receiver
已在A_Service
和Android Manifest
上以编程方式注册。这个电话看起来像这样:
在A_Service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
....
startBService()
...
}
public void startBService(){
if (!ServiceCheck.isRunning(B_Service.class, getBaseContext())){
Intent startIntent = new Intent();
startIntent.setClass(this, B_Broadcast_Receiver.class);
startIntent.putExtra(ServiceConstants.PARENT_ACTIVITY_NAME, parentActivity);
startIntent.putExtra(ServiceConstants.PARENT_SERVICE_NAME, parentService);
//Send broadcast to start the B Wakeful BroadcastReceiver
sendBroadcast(startIntent);
}
}
Wile调试我发现在onReceive()
调用后应用程序永远不会到达唤醒广播接收器B_Broadcast_Receiver
的{{1}}。
在sendBroadcast(startIntent)
:
B_Broadcast_Receiver
我对为什么会发生这种情况感到困惑,因为我以与主要活动几乎相似的方法开始服务public class B_Broadcast_Receiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context,B_Service.class);
service.putExtra(ServiceConstants.PARENT_ACTIVITY_NAME,intent.getStringExtra(ServiceConstants.PARENT_ACTIVITY_NAME));
service.putExtra(ServiceConstants.PARENT_SERVICE_NAME,intent.getStringExtra(ServiceConstants.PARENT_SERVICE_NAME));
Log.d("Wakeful LS", "Gonna start B service");
startWakefulService(context, service);
}
。
感谢您对此的帮助!
Android Manifest
A_Service