How to stop a service from BroadcastReceiver in Android? -
I have a broadcast receiver that receives the JSON of a cloud messaging (GCM) when passing a parameter, whose According to the beginning of a global objective of service class, the location of the cell is sent to the server every time the service is served, then another message is sent to the same GCM to stop it, the problem is that when I stop the service I am the service is zero, but I Also sends location. I want to stop sending location to stop service
BroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {string tag = "MainActivity". ServiceOGPS Service 1; Int nroService; Override public zero at @Receive (reference reference, intention intent) {// == I get JSON bundle extras = intestine .getExtras (); // == I check that if I want to start the service (("status") of extra consents) {service1 = new service GPS (reference); NroService = service1.onStartCommand (intent, 0, 58); Logs. I (tag, string.valouf (NRO service)); } Else {// == I check that if I want to stop the service (if extras.containsKey ("stop")) {// == Error in this line (Service 1 is NULL) service1.stopSelf () ; // == The current error in this line (service1 is zero)} and {// == I create an information component comp = new component name (context.getPackageName (), GcmIntentService.class.getName ()); Logs. I (Tag, "Entro Al and Dale Broadcast"); // Keeping the device awake keeps the service started StartWakefulService (context, (intent.setComponent (COMP)); SetResultCode (Activity.RESULT_OK); }}}
}
List of errors
07-31 19: 26: 44.115: D / Android runtime (32 9 2): Shutdown VM 07-31 1 9: 26: 44.115: W / DelWiki (3292): thread = 1: thread with no exception (Group = 0x41 C0000) 07-31 1 9: 26: 44.120: e / Android Rentman (32 9 2): fatal exception: main 07-31 19: 26: 44.120: e / AndroidRuntime (3292): java.lang.RuntimeException: java.lang.NullPointerException 07-31 19:26: Unable to start receiver com.ubaldino.demo_gcm.GcmBroadcastReceiver: 44.120: e / AndroidRuntime (3292): on android.app.ActivityThread.handleReceiver (ActivityThread.java:2541) 07-31 19: 26: 44.120: E / AndroidRuntime (3292): android App.ActivityThread.access $ 1600 (ActivityThread.java:159) 07-31 19:26: 44.120: E / Android Random (3292): Android. App Activitrif $ H Hondal Message (ActivityTrad.JAVPROSHOR 29) 07-31 9: 26: 44.120: E / Android Random (3292): Android.OS Handler Dispect Message (Handler. Java: 99) 07-31 19: 26: 44.120: E / Android Time Time (3292): One on Droid.os.Looper.loop (Looper.java:176) 07-31 19: 26: 44.120: E / Android Random (3292): Android. App ActivateTrade.Mine (ActivityTrad.Java1919) 07-31 19: 26: 44.120: E / Android Rendime (3292): at java.lang.reflect.Method.invokeNative (Native Methods) 07-31 19: 26: 44.120: E / Android Runtime (3292): on java.lang.reflect.method. Call (Method.java:525) 07-31 19: 26: 44.120: E / AndroidRuntime (3292): com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1046) 07-31 19: 26: 44.120: E / AndroidRuntime (3292): 26 :: 44.120: E / AndroidRuntime (3292): com.android.internal.os.ZygoteInit.main (ZygoteInit.java:862) on dalvik.system 07-31 at 19 KNativeStartkmain (Basic Law) 07-31 1 9: 26: 44.120: e / Android Rentaim (32 9 2) by reason: java.lang.NullPointerException 07-31 19: 26: 44.120: e / Androidrentaim (32 9 2): .ubaldino.demo_gcm.GcmBroadcastReceiver.onReceive (GcmBroadcastReceiver.java:34) com 07-31 19: 26: 44.120: e / AndroidRuntime (3292): on android.app.ActivityThread.handleRece Iver (ActivityThread.java : 2525) 07-31 19: 26: 44.120: E / Android Random (32 9 2): ... 10 more
You do not directly call the diastore or any other lifecycle function. To stop any service, use context.stopService ()
to start the service, you use context.startService (). I'm not sure what you are trying to do, but it will not really start the service and if it is not just crashing then it will cause problems with the structure.
Comments
Post a Comment