Android: how to get the intent received by a service? - android

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.

onStart() is deprecated now. You should use onStartCommand(Intent, int, int) instead.

Override onStart() -- you receive the Intent as a parameter.

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);
...
}
...
}

Related

The intent is on null in service when getting data from receiver

I have develop simple app for phone state listner for that I am starting service when call is in incoming mode and idle mode it is working fine but I want to pass data from broadcast receiver to my service I am starting my service from broadcast like this
Intent reivToServ = new Intent(context, RecorderService.class);
reivToServ.putExtra("number",phoneNumber);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
context.startForegroundService(reivToServ);
}
else
{
reivToServ.putExtra("number", phoneNumber);
context.startService(reivToServ);
}
and for getting the data inside service
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("command",intent.getStringExtra("number"));
}
But the intent is on null please any solution for this

Why Android app crash when close app if I getExtras in Service?

In Activity I start a service
Intent serv=new Intent(this, MyService.class);
serv.putExtra("mac", "mac");
startService(serv);
In the service, I get the parameter
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String mac=intent.getExtras().getString("mac");
return super.onStartCommand(intent, flags, startId);
}
Then I kill the app, the app will crash, and the service will also crash.
If I remove this line, then it will be no problem, the service also alive after I killed the app.
String mac=intent.getExtras().getString("mac");
Why the app crash?
super.onStartCommand(intent, flags, startId) returns the START_STICKY flag by default which, according to the docs, mean that:
"if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent."
Since the Intent isn't being redelivered you likely get NPE when calling intent.getExtras().
To redeliver the Intent return the START_REDELIVER_INTENT flag from onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
return START_REDELIVER_INTENT;
}
You are putting extras to wrong intent bleService.putExtra("mac", "mac");
Instead you should write serv.putExtra("mac", "mac");
Also you should always check if there are extras
if(getIntent().hasExtra("mac"){
//do some stuff
}

My Android service does not start

My service does not start.
I used the following onCreate method within my service:
public void onCreate(Bundle savedInstanceState){
wsConnection = new WSConnection();
handler = new Handler();
Bundle b = savedInstanceState;
this.dbManager = b.getParcelable("object");
super.onCreate();
Log.i("prova","servizio attivato");
TimerTask task = new TimerTask(){
public void run(){
FindPendingData();
}
};
timer = new Timer();
//timer.schedule(task, cal.getTime());
timer.schedule(task, 0, 30000);
}
while, the following code starts my service:
Intent intent = new Intent(this, myService.class);
intent.putExtra("object", myObject);
startService(intent);
object extends Object and implements Parcelable
I also declare in the Manifest:
<service android:name="myPackage.myService" />
Any idea Where the problem may be?
EDIT: I tried removing the Bundle parameter and now the service starts! But I don't understand why! Someone can help me?
use onStartCommand(Intent, int, int) or onStart() instead of onCreate of Service for getting Intent data as:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// get intent data here
return START_STICKY;
}
as in comment OP says :
no error. I always use this code for starting service and usually
this works good
but if we see in doc onCreate() not take any parameter if you are overriding it

Android Service Activity Intent

I want to pass a string from activiy to service.
Bundle mBundle = new Bundle();
mBundle.putString("MyString", string);
mIntent.putExtras(mBundle);
startService(mIntent);
this is in Activity class
Intent myIntent = getIntent();
String value = myIntent.getExtras().getString(key);
and this is in Service class
It doesn't accept getIntent() method :S I don't know what I'll do
The code in the service must be placed in onStart(Intent intent, int startid) method and the code becomes String value = intent.getExtras().getString(key);
When you start the service using startService(mIntent) the service's onStartCommand is called which is good place to handle the intent.
Move the part of your code that depends on the intent to onStartCommand: http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)
OnStartCommand was called OnStart before api version 5, follow link to documentation for further information about backwards compatibility in your app.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String value = intent.getExtras().getString(key);
}
Also remember to move heavy code into a background thread that you start in onStartCommand, as otherwise you will run into an Application Not Responding error.

Pass String from activity to service?

I am familiar with the method of passing arrays from one activity to another activity using putExtra and getExtra methods. However, whenever I try to get it from a service the following code doesn't work:
Bundle b = this.getIntent().getExtras();
String Array = b.getStringArray("paths");
It is not recognizing the following:
this.getIntent().getExtras();
Any ideas?
EDIT
in the activity class I have the following:
toService = new Intent();
toService.setClass(this, Service.class);
toService.putExtra("paths",Array);
in the service class:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
if(extras!=null)
{
Paths = extras.getStringArray("paths");
Toast.makeText(protectionService.this, Paths[0], Toast.LENGTH_SHORT).show();
}
return 0;
}
Nothing is appearing since Paths is not being assigned apparently.
Paths = extras.getStringArray("paths");
Doesn't seem to work.
Where are you trying to access getIntent?
Here is a snippet from a program I have written which uses the getExtras:
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Bundle extras = intent.getExtras();
if (extras != null) {
// Do what you want
}
}
However, onStart is now deprecated so you should really use onStartCommand.
You get the intent as one of the parameters.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Otherwise you could use AIDL, Preferences or any other example answered here: How to access a variable present in a service
Same question has already been answered Android: how to get the intent received by a service?
Edit:
If you use this
toService = new Intent();
toService.setClass(this, Service.class);
toService.putExtra("array",Array);
You need to get the extras with the same key, here the key is "array"
Paths = extras.getStringArray("array");

Categories

Resources