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

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
}

Related

When android service is restarted by the OS due to Sticky flag, is intent passed to it?

public int onStartCommand(Intent intent, int flags, int startId) {
}
When a service is started, I pass an intent to startService function. When service is restarted by the OS due to Sticky flag, is that very same intent that I passed to it to start the service be passed to onStartCommand again or will it be null?
Intent is only passed again if you request START_REDELIVER_INTENT
What is START_STICKY,START_NOT_STICKY and START_REDELIVER_INTENT Service

Android does service stopSelf() still require return

Is it necessary to call return; if I have decided to call stopSelf() command in a onStartCommand() within a service?
Yes. The method must have a return statement because the signature for onStartCommand() indicates that it returns a value:
public int onStartCommand (Intent intent,
int flags,
int startId)

Timer inside Android service

I have an Android service that starts a timer that does stuff:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer.scheduleAtFixedRate(new VeryImportantTask(), 0, RATE);
return START_STICKY;
}
I know Services are singleton but, does onStartCommand method called each time that I call startService()? If so, I should control that my timer is just started the first time, shouldn't I? I'm thinking in a static boolean flag in the service. Is there a better way?
In your case i.e START_STICKY you can simply check the intent value nullity
like this
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(null==intent){
// service restarted do what you want
}
return START_STICKY;
}
because first time the intent will not be null and it will be null every time in case of a restart with START_STICKY.

Android service is restarting slowly

I have created a service and returned "START_REDELIVERY_INTENT" in "onStartCommand()" method, as i want to run the service even after destroying the App's MainActivity.
App should restart just after destroying the App with previous non-null intent. But App is taking some time to restart. We can see the status of restarting App in device setting (Below is snapshot). This snapshot is after destroying App..... What i do? Any Help please?
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
Log.d(TAG, "intent: "+intent);
return START_REDELIVER_INTENT;
}

Restarting Services in Android

In android application if I save the intent of the service that is running. How can I restart the service?
I want it to run onStartCommand function again.
Thanks
Make onStartCommand return START_STICKY .
public int onStartCommand(Intent intent, int flags, int startId){
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
Documentation link: http://developer.android.com/reference/android/app/Service.html

Categories

Resources