Service does not restart after "Clear Memory" + appWidget crashes

I created an appWidget that registers some services in the onEnabled () method.

The problem is that after I use the built-in Clean Memmory / Ram task manager, the appWidget application will work (all AppWidgets TextView text files are installed by default (TextView)), and the services stop working and never restart.

This only happens after a while when the widget is installed, and if I clean Memmory / Ram immediately after installing the widget, an error will not occur, but I think that this is due to the task manager method of cleaning the RAM.

So finally, my question is: is there a way so that the Android system can restart these services? since the other applications that I downloaded through the market seem to continue to work after this procedure.

I will be happy for ideas and solutions! Thanks, advanced, Gal :)

some code i use:

onEnabled () method in 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 (other services are very similar, since 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(); } 
+3
source share
3 answers

After a lot of research and some attempts, I decided! :)

BroadcastReciever just added, listening to package changes and updates:

register the receiver in the manifest file:

 <receiver android:name=".receivers.onRestartReciever"> <intent-filter> <action android:name="android.intent.action.PACKAGE_REPLACED" /> <action android:name="android.intent.action.PACKAGE_RESTARTED" /> <data android:scheme="package" android:path="my.Package.Path" /> </intent-filter> 
  • PACKAGE_REPLACED - called, in particular, to notify of an application update.
  • PACKAGE_RESTARTED - called when most memmory cleaners clear memory.
  • the string "data" is used to monitor the actions applied for a particular package.

Inside this receiver, I start my services again and restart the widget view (in this case, its onUpdate () method is called):

 public class onRestartReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Log.d("DEBUG", "onRestartReciever"); // Register Services MyWidget_1x1.registerServices(context); MyWidget_2x2.registerServices(context); // reInitialize appWidgets AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(context); MyWidget_1x1 widget1x1=new CallWidgi(); widget1x1.onUpdate (context, AppWidgetManager.getInstance(context), widget1x1.getIDs(context, appWidgetManager)); MyWidget_2x2 widget2x2=new CallWidgi2(); widget2x1.onUpdate(context, AppWidgetManager.getInstance(context), widget2x2.getIDs(context, appWidgetManager)); } } 

registerServices (Context) is a static method in my AppWidgetProvider classes (MyWidget_1x1 / MyWidget_2x2) that registers the necessary services.

Hope this helps you too =]

+9
source

The above content captures a reload event, but it is not possible to catch an explicit memory event because the file android.intent.action.PACKAGE_RESTARTED is not sent to the package itself.

I solved this by adding a Service. This service gets its onCreate () caused by an explicit memory event. Note that you need to start the service by calling startService, which I call from AppWidgetProvider.

 Intent i= new Intent(context, CTService.class); // potentially add data to the intent i.putExtra("KEY1", "Value to be used by the service"); ComponentName name = context.startService(i); 

The service code is similar:

 @Override public void onCreate() { Log.d(TAG,"onCreate"); ping(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG,"onStartCommand: "+intent.getAction()); //TODO do something useful ping(); return Service.START_STICKY; } 

with ping actual stuff doing touch events on widgets.

+1
source

Thank you for successfully researching your own solution. You pointed me in the right direction. I had a similar problem when appwidget no longer updated after a reboot or power cycle, and it no longer responds to button presses.

What I did first added the following to the manifest:

 <receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> 

Then I created a BootReceiver.java file with something like the following. In my case, I had to recreate the alarm and expectations for the buttons.

 public class BootReceiver extends BroadcastReceiver { /* * after reboot widget appears to stop working and becomes unresponsive to clicks * this broadcast receiver will create new alarm and refresh pending intents */ @Override public void onReceive(Context context, Intent intent) { /* do your stuff here, mostly just copy&paste from other places */ } } 

Some credit for this also applies to: Proximity alerts do not work after rebooting the phone.

0
source

Source: https://habr.com/ru/post/1434697/


All Articles