I have a widget that simply uses a service to update the looks of a widget's RemoteViews. The service is started based on filters supplied by a different registered BroadcastReceiver. When one of the c开发者_开发技巧riteria are met, the onReceive() of my BroadcastReceiver is called and I start my service. The service runs and then stops itself. This works great for a while. However, after a while it is as if the BroadcastReceiver is killed. I have seen in the logs where my process has died, and it restarts my service in 5000 ms. This is great, but the problem is that my BroadcastReceiver is what actually controls the starting of the service based on my subscribed events. Without it, my service is not started when those events occur, such as the user's screen on. Any idea why this would happen? Why would the BroadcastReceiver stop receiving events when the unRegister() has not been called? I don't want my service to be up and running at all times. I suspect the same behavior would occur if I used the service itself as the BroadcastReceiver. Thanks for any help.
@Override
public void onReceive(Context context, Intent intent)
{
try
{
Log.i(TAG, "Received Broadcast: " + intent.getAction());
Bundle bundle = intent.getExtras();
context.startService(new Intent(
com.mypkg.services.UpdateService.ACTION_UPDATE));
Log.i(TAG, "Service start complete.");
}
catch(Throwable t)
{
JLog.e(TAG, "An occurred during onReceive(): ", t);
}
}
Your application is crashing, you'll need to find the crash dump in the logs to understand more about it.
You can make the broadcast receiver immune by not needing to registerReceiver() in the first place -- instead of programatically declaring the receiver, declare it in your AndroidManifest, along with an appropriate intent filter to define the broadcast(s) you wish to receive.
精彩评论