I'm starting a service with an intent where I put extra information.
How can I get the intent in the code of my service?
There isn't a function like getIntent().getExtras()
in service like in activity.
Intent ir=new Intent(this, Service. class); ir. putExtra("data", data); this. startService(ir);
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work.
Override onStart()
-- you receive the Intent
as a parameter.
onStart()
is deprecated now. You should use onStartCommand(Intent, int, int)
instead.
To pass the extras:
Intent intent = new Intent(this, MyService.class);
intent.putExtra(MyService.NAME, name);
...
startService(intent);
To retrieve the extras in the service:
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
String name = intent.getExtras().getString(NAME);
...
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With