I've built an appWidget which register some services on his onEnabled() method.
The problem is that after I use the built in Task Manager's Clean Memmory/Ram, the appWidget view crashes (all the appWidgets TextView's text is set to default (TextView)) and the Services stop running and never restarts.
This only happen after some time the widget is set, and if I Clean Memmory/Ram right after the widget is set the bug does'nt happen, but I guess this is related to the Task Manager's method of cleaning RAM.
So finally, my question is: Is there a way to tell the android system to reStart those services? as other appWidgets I've downloaded through the market is seem to continue working fine after this procedure.
Will be happy for ideas and solutions! Thanks advanced, Gal :)
some code that I use:
the onEnabled() method in the appWidget:
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Intent newinIntent = new Intent(context, CallService_1x1.class);
context.startService(newinIntent);
newinIntent = new Intent(context, SmsService_1x1.class);
context.startService(newinIntent);
}
Some methods from one of the Services (others services are very similiar as this is from their abstract method):
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
Log.d("SERVICE-SMS","CallMonitorService - onStartCommand created");
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
context = this.getApplicationContext();
Log.d("SERVICE-SMS","CallMonitorService created");
registerObserver();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
unregisterObserver();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
/**
* Start the service to process that will run the content observer
*/
public static void beginStartingService(Context context) {
Log.d("SERVICE-SMS","CallMonitorService: beginStartingService()");
context.startService(new Intent(context, CallService.class));
}
/**
* Called back by the service when it has finished processing notifications,
* releasing the wake lock if the service is now stopping.
*/
public static void finishStartingService(Service service) {
Log.d("SERVICE-SMS","CallMonitorService: finishStartingService()");
service.stopSelf();
}
The stuff above does catch the reboot event but fails to catch the clear memory event, because the android.intent.action.PACKAGE_RESTARTED is not sent to the package itself.
I solved this by adding a Service. This service gets its onCreate() called on a clear memory event. Note that you have to start the service with a startService call, which I call from the AppWidgetProvider.
The Service code is like:
with
ping
the actual stuff which makes the touch events on the widgets work.After a lot of research and some attempts, Solved it! :)
Just added BroadcastReciever listening to package changes and updates:
register receiver in the manifest file:
Within this reciever I start my services again, and restarts the widget view (calling it's onUpdate() method in this case):
registerServices(Context) is a static method in my AppWidgetProvider classes (MyWidget_1x1/MyWidget_2x2) which registers the needed services.
Hope it will help you too =]
Thanks for successfully researching your own solution. You pointed me in the right direction. I was having a similar problem where the appwidget would not update anymore after a reboot or power cycle, neither would it respond to button clicks anymore.
What I did was first add the following to the manifest:
Then I created a BootReceiver.java file with something like the following. In my case I had to recreate the alarm and pending intents for the buttons.
Some credit for this also goes to: Proximity Alerts not working after phone reboot