
我有一个broadcastReceiver,它在传递参数时接收Google Cloud Messaging(GCM)的JSON,根据该参数,服务正在开始该类的全局对象,服务每隔一段时间就将该单元的位置发送到服务器,然后在同一个GCM上向他发送了另一条消息来阻止他,问题是当我停止服务时说这项服务是NulL,但仍然发送该位置.我想停止服务停止发送位置.
广播接收器
public class GcmbroadcastReceiver extends WakefulbroadcastReceiver {String TAG = "MainActivity";ServicioGPS service1;int nroService;@OverrIDepublic 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 VM07-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: main07-31 19:26:44.120: E/AndroIDRuntime(3292): java.lang.RuntimeException: Unable to start receiver com.ubaldino.demo_gcm.GcmbroadcastReceiver: java.lang.NullPointerException07-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.access00(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.NullPointerException07-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解决方法:
您永远不会直接调用onDestroy或任何其他生命周期功能.要停止服务,请使用context.stopService()
另外,要启动服务,请使用context.startService().我不确定你要做什么,但它不会真正启动服务,如果它不会崩溃,将导致框架出现问题.
总结以上是内存溢出为你收集整理的如何从Android中的BroadcastReceiver停止服务?全部内容,希望文章能够帮你解决如何从Android中的BroadcastReceiver停止服务?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)