I have a DashClockExtension
that sometimes doesn't update.
It's using a LocalBroadcastReceiver to update the extension similar to http://bit.ly/1e4uMl0. The receiver is registered in the onInitialize()
method:
@Override
protected void onInitialize(boolean isReconnect) {
super.onInitialize(isReconnect);
LocalBroadcastManager broadcastMgr = LocalBroadcastManager.getInstance(this);
if (mDashClockReceiver != null) {
try {
broadcastMgr.unregisterReceiver(mDashClockReceiver);
} catch (Exception ignore) {}
}
mDashClockReceiver = new DashClockUpdateReceiver();
broadcastMgr.registerReceiver(mDashClockReceiver, new IntentFilter(UPDATE_DASHCLOCK));
}
That's how I send the broadcast:
public static void updateDashClock() {
LocalBroadcastManager.getInstance(Application.getContext()).sendBroadcast(new Intent(UPDATE_DASHCLOCK));
}
That's my BroadcastReceiver:
private class DashClockUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
retrieveDataAndUpdateWidget();
}
}
I noticed that while the broadcast is triggered the receiver doesn't receive the event sometimes.
I tested it by killing my app but that doesn't reproduce the issue so I'm at a loss why this would happen.
Anyone?
Please take a look at how the GCM broadcast receiver is implemented in Google's GCM Client example.
It might be because your application was killed by the Android OS because it needed more memory (or for whatever reason) and then you will not receive the broadcasts in your broadcast receiver.
I hope it helps, if not, please provide us with some logs and more information.
Best of luck!
After running extensive tests, I got some results that might be interessting to other developers too:
Here's my workaround:
Here's how I disable, enable the service:
configureComponent("mypackagename.DashClockService", false); // disable
configureComponent("mypackagename.DashClockService", true); // enable
private void configureComponent(Context context, String className, boolean enable) {
PackageManager pkMgr = context.getPackageManager();
String packageName = context.getPackageName();
ComponentName component = new ComponentName(packageName, className);
int newState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
pkMgr.setComponentEnabledSetting(component, newState, PackageManager.DONT_KILL_APP);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With