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();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…