2

I'm working on building a Location_Service for my Android App.I have a Service say , T_Service that starts on phone boot and checks for few conditions in its onStartCommand() to start my Location_Service. I want to send these conditions as an intent from a broadcast receiver associated to with the T_Service.

I do the following:

In the Broadcast Receiver

 public class T_BroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                Intent i = new Intent(context,T_Service.class);
                i.putExtra("PARENT_ACTIVITY_NAME","com.example.helloworld");
                context.startService(i);
            }
        }

In the T_Service class:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
     Log.d("APP","The intent has parent activity : "+intent.getExtras().getString("PARENT_ACTIVITY_NAME"));
        }

I get a NullPointerException when my code reaches the above point that tries to print the value associated with the key that was set in the Intent.

Kindly advise me on how to go about the case of adding extras to an intent of a Service like T_Service that starts on phone boot.

The Stack trace for the Exception :

FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start service org.abc.def.services.TService@41593b58 with null: java.lang.NullPointerException
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2387)
            at android.app.ActivityThread.access$1900(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at org.abc.def.services.TrackerService.onStartCommand(TrackerService.java:140)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2370)
            at android.app.ActivityThread.access$1900(ActivityThread.java:127)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1221)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
            at dalvik.system.NativeStart.main(Native Method)
vardhinisuresh27
  • 371
  • 2
  • 6
  • 18

5 Answers5

1

Please read the javadoc for onStartService. It says that the intent parameter can be null, so you should always check it for null before calling methods on it.

My experience is that even when you think that an IntentSevice will never receive a null there, it is actually still possible in extremely rare circumstances. So you should still check it for null even if you don't think it will happen.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
1

replace this

 @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
   Log.d("APP","The intent has parent activity : "+intent.getExtras().getString("PARENT_ACTIVITY_NAME"));
    }

with this

@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
     super.onStartCommand(intent, flags, startId);

     Bundle b = getIntent().getExtras();
      if (b != null){
     Log.d("APP","The intent has parent activity :"+b.getString("PARENT_ACTIVITY_NAME")); }
Nitesh
  • 318
  • 3
  • 16
1

From the documentation (onStartCommand):

Parameter "intent": The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.

If you're running this on phone boot is likely that you will get null on the intent because the process that started it has gone away.

To receive the the same intent you would need to return START_REDELIVER_INTENT in the onStartCommand method, as follows:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("APP","The intent has parent activity : "+intent.getExtras().getString("PARENT_ACTIVITY_NAME"));

    return Service.START_REDELIVER_INTENT;
}

Returning START_REDELIVER_INTENT will make the intent parameter to be passed again to the onStartCommand method whenever the service get restarted (e.g on phone boot).

For more details check the documentation.

START_REDELIVER_INTENT

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int)

kevinkl3
  • 961
  • 7
  • 22
0

Please check your intent and extra string before accessing. May be it is null

if(intent.getExtras()!=null && intent.hasExtra("PARENT_ACTIVITY_NAME")){
 Log.d("APP","The intent has parent activity : "+intent.getExtras().getString("PARENT_ACTIVITY_NAME"));
}

To pass an intent, please refer older thread.

Pass data from Activity to Service using an Intent

Community
  • 1
  • 1
Rakesh
  • 756
  • 1
  • 9
  • 19
  • Thanks! My focus is on the occurence of the `NullPointerException` when I've already set the Extras for the intent in the broadcast receiver that is associated with the service. – vardhinisuresh27 Feb 11 '16 at 04:54
  • @vardhinisuresh27 Checking for null and the presence of the extra will keep the program from crashing...but it won't solve the problem OP asked which was **"Kindly advise me on how to go about the case of adding extras to an intent of a Service like T_Service that starts on phone boot."** – rothloup Feb 11 '16 at 05:19
0

Per the Intent API documentation, you must include a package name in the extra's name.

public Intent putExtra (String name, String value)

Added in API level 1 Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".

Parameters name The name of the extra data, with package prefix. value The String data value. Returns Returns the same Intent object, for chaining multiple calls into a single statement. See Also putExtras(Intent) removeExtra(String) getStringExtra(String)

rothloup
  • 1,220
  • 11
  • 29