Android: service restart detection using START_REDELIVER_INTENT

My widget has a service associated with it to handle various click commands that are transmitted as it wishes. I also set return START_REDELIVER_INTENT; for the case when the service is restarted so as not to throw a nullPointerException when calling aim.getAction (); etc. The problem is that when the last intention was sent by one of the setOnClickPendingIntent calls that I have, then when the service restarts, it acts as if the user clicked on one of the viewId. eg.

 String command = intent.getAction(); int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID); if(command.equals("some command")){ //do something } ... remoteView.setOnClickPendingIntent(R.id.viewId,MyClass.makeControlPendingIntent(getApplicationContext(),"some command",appWidgetId)); 

Where makeControlPendingIntent:

 public static PendingIntent makeControlPendingIntent(Context context, String command, int appWidgetId) { Intent active = new Intent(context,MyService.class); active.setAction(command); active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); Uri data = Uri.withAppendedPath(Uri.parse("myclass://widget/id/#"+command+appWidgetId), String.valueOf(appWidgetId)); active.setData(data); return(PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT)); } 

Is there something I can do to check if the service restarts, so as not to run any of these commands in case Android restarts my service?

+4
source share
2 answers

Is there something I can do to check if the service restarts, so as not to run any of these commands in case Android restarts my service?

The flags parameter for onStartCommand() will contain START_FLAG_REDELIVERY if the Intent is re-set. So you can do something like this:

 public int onStartCommand(Intent intent, int flags, int startId) { if ((flags & START_FLAG_REDELIVERY)!=0) { // if crash restart... // do something here } // rest of logic here } 
+17
source

You should be able to simply check if the intent is null to find out if it is rebooted.

0
source

All Articles