阅读更多

 

03-02 17:15:44.770   536   778 W ActivityManager: Scheduling restart of crashed service ****/.service.WatchDogService in 5000ms03-02 17:15:44.770   536   778 W ActivityManager: Scheduling restart of crashed service ****/.service.APPTestService in 15000ms03-02 17:15:44.770   536   778 W ActivityManager: Scheduling restart of crashed service 

   进程被杀掉,系统自动把Service重启

 

   

 

03-02 17:15:49.909 24415 24415 E AndroidRuntime: FATAL EXCEPTION: main03-02 17:15:49.909 24415 24415 E AndroidRuntime: java.lang.RuntimeException: Unable to start service ***.service.ScanAppService@411d5438 with null: java.lang.NullPointerException03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2894)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.app.ActivityThread.access$1900(ActivityThread.java:166)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.os.Looper.loop(Looper.java:194)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5427)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)03-02 17:15:49.909 24415 24415 E AndroidRuntime: Caused by: java.lang.NullPointerException03-02 17:15:49.909 24415 24415 E AndroidRuntime: at ***.service.AppCollectService.onStartCommand(Unknown Source)03-02 17:15:49.909 24415 24415 E AndroidRuntime: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2877)03-02 17:15:49.909 24415 24415 E AndroidRuntime: ... 10 more

   经查看onStartCommand中代码,只有intent为空才可能报NullPointException

 

 原因:

intent的参数是null,原因是这个intent参数是通过startService(Intent)方法所传递过来的,但是如果Service在你的进程退出后有可能被系统自动重启,这个时候intent就会是null.
解决方法:
所以在使用intent前需要判断一下是否为空
还有另外一种解决方法:
如果实现 onStartCommand去调度异步工作或者其他的线程,有必要设置START_FLAG_REDELIVERY让系统重发intentservice以便service被killed后不会丢失intent数据。

 

return super.onStartCommand(intent, Service.START_REDELIVER_INTENT, startId);

 google 文档:

 

 

public static final int START_NOT_STICKYAdded in API level 5Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate until a future explicit call to Context.startService(Intent). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will not be re-started if there are no pending Intents to deliver.This mode makes sense for things that want to do some work as a result of being started, but can be stopped when under memory pressure and will explicit start themselves again later to do more work. An example of such a service would be one that polls for data from a server: it could schedule an alarm to poll every N minutes by having the alarm start its service. When its onStartCommand(Intent, int, int) is called from the alarm, it schedules a new alarm for N minutes later, and spawns a thread to do its networking. If its process is killed while doing that check, the service will not be restarted until the alarm goes off.Constant Value: 2 (0x00000002)public static final int START_REDELIVER_INTENTAdded in API level 5Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). This Intent will remain scheduled for redelivery until the service calls stopSelf(int) with the start ID provided to onStartCommand(Intent, int, int). The service will not receive a onStartCommand(Intent, int, int) call with a null Intent because it will will only be re-started if it is not finished processing all Intents sent to it (and any such pending events will be delivered at the point of restart).Constant Value: 3 (0x00000003)public static final int START_STICKYAdded in API level 5Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.Constant Value: 1 (0x00000001)

 

 

更多相关文章

  1. Android与服务器传递数据
  2. Android之canvas详解
  3. android listview 三种适配器设置
  4. Android(安卓)camera调用出现错误解决方法
  5. 常见问题汇总一
  6. ListView.setOnItemClickListener、setOnCreateContextMenuListe
  7. Android(安卓)Bander设计与实现 - 设计篇
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. Python技巧匿名函数、回调函数和高阶函数

随机推荐

  1. Android(安卓)Studio 使用二三事----' '
  2. One省电卫士 - Android内核级省电App
  3. Android自定义实现圆形播放进度条
  4. 【Android】私有目录获取方法
  5. Android(安卓)Touch 触摸事件
  6. [原创] Android(安卓)GPS (当前位置 & GPS
  7. android 控制按钮各个状态的样式
  8. Android(安卓)实现文件上传功能(upload)
  9. Windows平台下如何使用Android(安卓)NDK
  10. Android系统原理及开发要点详解