Passing data to the onDestroy () service

I want to know if the service was terminated from a specific action, so I pass a line from this action when calling stopServivce(service) .

Here is the code:

 Intent service = new Intent(Activity.this, service.class); service.putExtra("terminate", "activity terminated service"); stopService(service); 

But I can access this variable using getIntent().getExtras().getString("terminate); in the onDestroy() method.

[EDIT]

I found a way to overcome this obstacle, but I would like my question still to be given. I just did everything I needed to do in the onDestroy() method in action, and then called stopService(service) . I was lucky that I did not need anything more complicated.

+6
source share
3 answers

There is no way to access Intent in onDestroy . You must signal the service in another way (Binder, Shared Preferences, Local Broadcast, Global Data or Messenger). A good example of using translation is given in this answer . You can also make this work by calling startService instead of stopService . startService only starts new services, if they are not already, so several calls to startService are the mechanism for sending an Intent to a service. You see that this trick is used by BroadcastReceivers . Since you have access to Intent in onStartCommand , you can implement termination by checking the advanced Intent options and calling stopSelf if you are prompted to exit. Here is his sketch in action -

 public int onStartCommand(Intent intent, int flags, int startId) { final String terminate = intent.getStringExtra("terminate"); if(terminate != null) { // ... do shutdown stuff stopSelf(); } return START_STICKY; } 
+11
source

Just to illustrate what iagreen offers;

In action

 Intent broadcastIntent = new Intent(); broadcastIntent.setAction("com.package.yourfilter"); broadcastIntent.putExtra("activity_name", "your_activity"); sendBroadcast(broadcastIntent); 

In the service

  private YourActionReceiver abc; this.abc = new YourActionReceiver(); registerReceiver(this.abc, new IntentFilter("com.package.yourfilter")); public class YourActionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Get the name of activity that sent this message } } 
0
source

Global status

- your friend.:)
Check the global string every time you need (say, before completion). You can also have a state enumeration. Or a flag to find out if the condition is real.

Recipe:

The more general problem you are facing is how to maintain state in several actions and in all parts of the application. A static variable (e.g. singleton) is the usual way to implement Java. However, I found that a more elegant way in Android is to associate your state with the application context.

As you know, each activity also represents a context that represents information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance in your application.

The way to do this is to create your own subclass of android.app.Application , and then point that class in the application tag to your manifest. Now Android will automatically instantiate this class and make it available to your entire application. You can access it from any context using the Context.getApplicationContext () method (Activity also provides the getApplication () method, which has the same effect):

 class MyApp extends Application { private String myState; public String getState(){ return myState; } public void setState(String s){ myState = s; } } class Blah extends Activity { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp)getApplicationContext()); String state = appState.getState(); ... } } class BlahBlah extends Service { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp)getApplicationContext()); String state = appState.getState(); ... } } 

This has the same effect as a static variable or singleton, but integrates pretty well into the existing Android platform. Note that this will not work in all processes (should your application be one of the rare ones that has several processes).

Credits go to @Soonil

-1
source

All Articles