Search code examples
androidbroadcastreceiverandroid-service

How to stop a service from BroadcastReceiver in Android?


I have a BroadcastReceiver which receives a JSON of Google Cloud Messaging (GCM) upon passing a parameter, according to which a service is beginning a global object of the class, the service sends the location of the cell to a server every so often, then sent him another message on the same GCM to stop him, the problem is that when I stop the service says this service is NULL, but still sends the location. I want to stop the service to stop sending the location.

BroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

String TAG = "MainActivity";
ServicioGPS service1;
int nroService;
@Override
public void onReceive(Context context, Intent intent) {
    // == I get the JSON
    Bundle extras= intent.getExtras();
    // == I check if I want to start the service
    if (extras.containsKey("Position")) {
        service1 = new ServiceGPS(context);
        nroService= service1.onStartCommand(intent, 0, 58);
        Log.i(TAG, String.valueOf(nroService));
    }
    else
    {
        // == I check if I want to stop the service
        if (extras.containsKey("stop")) {
            // == Exist error in this line(service1 is NULL)
            service1.stopSelf();
            // == Exist error in this line (service1 is NULL)
        }
        else
        {
            // == I Make a notification
            ComponentName comp = new ComponentName(context.getPackageName(),
                    GcmIntentService.class.getName());              
            Log.i(TAG, "Entro al else del broadcast");
            // Starts the service, keeping the device awake.
            startWakefulService(context, (intent.setComponent(comp)));
            setResultCode(Activity.RESULT_OK);
        }
    }

}

}

List of errors

07-31 19:26:44.115: D/AndroidRuntime(3292): Shutting down VM
07-31 19:26:44.115: W/dalvikvm(3292): threadid=1: thread exiting with uncaught exception             (group=0x41ce0700)
07-31 19:26:44.120: E/AndroidRuntime(3292): FATAL EXCEPTION: main
07-31 19:26:44.120: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to start  receiver com.ubaldino.demo_gcm.GcmBroadcastReceiver: java.lang.NullPointerException
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2541)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.app.ActivityThread.access$1600(ActivityThread.java:159)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1392)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.os.Looper.loop(Looper.java:176)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.app.ActivityThread.main(ActivityThread.java:5419)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invoke(Method.java:525)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at dalvik.system.NativeStart.main(Native Method)
07-31 19:26:44.120: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException
07-31 19:26:44.120: E/AndroidRuntime(3292):     at com.ubaldino.demo_gcm.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:34)
07-31 19:26:44.120: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2525)
07-31 19:26:44.120: E/AndroidRuntime(3292):     ... 10 more

Solution

  • You never directly call onDestroy or any other lifecycle function. To stop a service, use context.stopService()

    Also to start a service, you use context.startService(). I'm not sure what you're trying to do, but it won't really start the service and will cause problems with the framework if it doesn't just crash.