StopService (intent_with_extras) - how do you read these add-ons from a service to stop?

I have a Service that can be stopped in several ways. Whenever I call stopService (Intent), I pass the intent with some additional functions. How to get these extra features?

Thanks.

+3
source share
5 answers

You need to override onStartCommand() in your Service this is how you get the link to the incoming intent from startService .

In this case, your intention would be to take a special action to force the service to stop on its own. You are adding additions to this intention, which can be read in onStartCommand() .

Sample code

 public class MyService extends Service { @Override public int onStartCommand(final Intent intent, final int flags, final int startId) { final String yourExtra = intent.getStringExtra(YOUR_EXTRA_ID); // now you can eg call the stopSelf() method but have the extra information } } 

explanation

Each time you call context.startService(Intent) onStartCommand() . If the service is already running, a new service is not created, but onStartCommand is still called. Here's how you can get a new intention with add-ons for a running service.

+6
source

I found that extra functions are not passed with intent when stopService is called. As a workaround, just call startService (Intent) and stopService (Intent) immediately after each other.

Example code from action:

Intent j = new Intent(SocketToYa.this, AccelerometerDataSocket.class); j.putExtra("com.christophergrabowski.sockettoya.DestroyService", true); startService(j); stopService(j);

In Service.onStartCommand,

intent.hasExtra("com.christophergrabowski.sockettoya.DestroyService")

will evaluate to true, and you will destroy your service as provided by the API (i.e. by calling stopService, which in turn will call OnDestroy after calling onStartCommand).

+4
source

My suggestion is that using a static member in a class that extends Activity to pass information for maintenance, and its as a regular static member in an outer class

Please do not do this unless you have another choice. You should try to use the mechanisms built into the structure to transfer data, and not use the public static fields if there is no other choice. Read Service documentation for examples.

+1
source

Can you use intent with the "shutdown" action with Context.startService ()?

That is, send the Intent with the shutdown action and additional functions to Service.onStartCommand (), decide how to exit based on the additional functions, and then use Service.stopSelf () to stop the service.

I agree that this is not a great solution, as it potentially launches a service to shut it down. I would still like to hear the β€œcorrect” way (if one exists) to do this using Context.stopService ().

+1
source

You can not write

getIntent ()

in the class extending the Service. Therefore, I think using getExtra () will not work. My suggestion is that using a static member in a class that extends Activity to pass information to the service, and its as regular static member access in an external class i.e.

Classname.yourobject

. see this link for another option http://developer.android.com/resources/faq/framework.html#3

-one
source

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


All Articles