我有一个 BroadcastReceiver,它接收 Google Cloud Messaging (GCM) 的 JSON,在传递参数时开始类的全局对象服务,该服务每隔一段时间向服务器发送单元格位置,然后在同一 GCM 上向服务器发送另一条消息以停止它。问题是,当我停止服务时,它会说这个服务为空,但仍然发送位置。我想停止服务以停止发送位置。
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);
}
}
}
错误列表
错误清单
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