阅读更多

 

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 系统(84)---查看分区的大小
  2. jni 获得 android 系统属性
  3. Android调用系统分享功能以及createChooser的使用
  4. android 应用异常可以引起android系统崩溃重启
  5. Android 系统自带的发送短信的界面源码
  6. android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConn
  7. Android 系统图形栈(一): OpenGL ES 和 EGL 介绍
  8. Android 调用系统摄像头

随机推荐

  1. android 数据库操作初试
  2. uri is not registered
  3. Android 使用zxing扫描二维码横屏转竖屏
  4. 小米5手机Android运行程序闪退出错解决方
  5. 携程Android App插件化和动态加载实践
  6. Android 查看SHA1值的方法
  7. Android中TextClock中的一个abc函数
  8. 从另一种方式理解Android消息处理机制
  9. Android实现类似有Table边框的布局
  10. Android 快速学习onTouch,并完成实验单点