What I am trying to do is once the AppWidget is removed from the homescreen of the user's Android phone, I want to stop the background service that Updates the AppWidget.
Here is the code that I am using...don't understand whats wrong?
@Override
public void onDeleted(Context context, int[] appWidgetIds){
Intent serviceIntent = new Intent(context, UpdateService.class);
context.stopService(serviceIntent);
super.onDeleted(context, appWidgetIds);
}
Any ideas? Thanks for the help.
After a lot of Research I finally fixed it. My goal here was to stop the background service if all AppWidget instances are removed from the screen.
This is what did it...
Used shared preferences to toggle 1 and 0. SharePref toggled to 1 when the very first instance of the widget is put on the screen by the user. It is toggled back to 0 when the last instance of the App widget is removed from the screen.
@Override OnReceive method in the Service. Listen for these broadcasts - AppWidget Enabled (broadcast when the very first instance of the widget is put on the screen) , AppWidget Disabled (broadcast by the OS when the very last instance of the widget is removed from the screen) When AppWidget Disabled is broadcast call the overridden method OnDisabled method.
3.In @Override OnDisabled method call stopService. Works perfect.
Remember the difference between OnDeleted and OnDisabled. OnDeleted is called when an instance of the widget is removed, it doesn't mean that the widget being deleted is the last one. I wanted my service to still run even if there was a single widget instance on the screen.
*Also if you do not want to do all of the above and let Android take care of the Service etc...use IntentService. **
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