This is my Service
getting called on button click from an Activity
. If I swipe my Activity
left while Service
is running it crashes. I have also tried running it in separate process by putting in android:process=":remote"
in the manifest but it is still the same.
@Override
public void onCreate(){
super.onCreate();
Log.d("Service", "Creating");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
@Override
public int onStartCommand (Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
Log.d("Started", "Service");
type = intent.getIntExtra("Type",-1);
mode = intent.getIntExtra("Mode",-1);
rank = intent.getIntExtra("Rank", -1);
latitude = intent.getDoubleExtra("Lat", -1.0);
longitude = intent.getDoubleExtra("Long", -1.0);
startTime = intent.getLongExtra("Start", 0);
endTime = intent.getLongExtra("End", 0);
The error I am getting is:
java.lang.RuntimeException: Unable to start service com.routofy.routofytest.MyLocationService@374afe93 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2913)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
at com.routofy.routofytest.MyLocationService.onStartCommand(MyLocationService.java:89)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2896)
at android.app.ActivityThread.access$2100(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1390)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
That is null
pointer on type = intent.getIntExtra("Type",-1);
I understand that killing the app is killing the process and Intent
this Service
is getting is coming out to be null
. But how to handle such cases that even if Activity
is killed Intent
is handed over to Service
?
I want service to be totally independent of Activity
. Also I am running it like:
Intent pickIntent = new Intent(getApplicationContext(),MyLocationService.class);
Foreground services are an advanced Android concept which allows you to display notifications to your users when running long lived background tasks. The notification acts like any other notification, however it cannot be removed by the user and lives for the duration of the service.
Poor memory management is one of the most common reasons for app crashes on mobile devices (tip: learn Android Studio Debug with Bugfender), and this is especially true for low-end mobiles and tablets.
A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.
The lifecycle of service can follow two different paths: started or bound. Started. Bound.
I understand that killing the app is killing the process and intent this service is getting is coming out to be null.
Since you haven't posted the return value of onStartCommand()
I believe that it's START_STICKY
meaning that the service is recreated by the system with null
Intent
passed in.
But how to handle such cases that even if activity is killed intent is handed over to service.
Just return START_REDELIVER_INTENT
flag, which means that:
"If the system kills the service after onStartCommand()
returns, recreate the service and call onStartCommand()
with the last intent that was delivered to the service."
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