Uba*_*man 3 android broadcastreceiver android-service
我有一个BroadcastReceiver,它在传递参数时接收Google Cloud Messaging(GCM)的JSON,根据该参数,服务正在开始该类的全局对象,服务每隔一段时间就将该单元的位置发送到服务器,然后在同一个GCM上向他发送了另一条消息来阻止他,问题是当我停止服务时说这项服务是NULL,但仍然发送该位置.我想停止服务停止发送位置.
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
错误列表
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
Run Code Online (Sandbox Code Playgroud)
您永远不会直接调用onDestroy或任何其他生命周期功能.要停止服务,请使用context.stopService()
另外,要启动服务,请使用context.startService().我不确定你要做什么,但它不会真正启动服务,如果它不会崩溃,将导致框架出现问题.
| 归档时间: |
|
| 查看次数: |
6221 次 |
| 最近记录: |