Android service with START_STICKY crash when killing application

This is my Service call when the Activity button is clicked. If I put my Activity left and the Service works, it will work. I also tried running it in a separate process by inserting android:process=":remote" into the manifest, but it's 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) 

This is a null pointer to type = intent.getIntExtra("Type",-1);

I understand that killing an application kills a process, and Intent this Service gets null output. But how to handle such cases that even if the Activity killed, the Intent is passed to the Service ? I want the service to be completely independent of Activity . I also run it like:

 Intent pickIntent = new Intent(getApplicationContext(),MyLocationService.class); 
+6
source share
1 answer

I understand that killing an application kills the process and the intent that this service receives goes beyond zero.

Since you did not post the return value onStartCommand() , I believe that it START_STICKY means that the service was recreated by a system with a passed null Intent .

But how to handle such cases that even if the action is killed, the target is transferred to the service.

Just return the START_REDELIVER_INTENT flag, which means that:

"If the system kills the service after returning onStartCommand() , re-create the service and call onStartCommand() with the last intention that was delivered to the service."

+3
source

All Articles