android Service onStartCommand intent为nullhttp://hold-on.iteye.com/blog/2024926
来源:互联网 发布:女生围巾品牌 知乎 编辑:程序博客网 时间:2024/06/17 01:12
- 03-02 17:15:44.770 536 778 W ActivityManager: Scheduling restart of crashed service ****/.service.WatchDogService in 5000ms
- 03-02 17:15:44.770 536 778 W ActivityManager: Scheduling restart of crashed service ****/.service.APPTestService in 15000ms
- 03-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: main
- 03-02 17:15:49.909 24415 24415 E AndroidRuntime: java.lang.RuntimeException: Unable to start service ***.service.ScanAppService@411d5438 with null: java.lang.NullPointerException
- 03-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.NullPointerException
- 03-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让系统重发intent到service以便service被killed后不会丢失intent数据。
- return super.onStartCommand(intent, Service.START_REDELIVER_INTENT, startId);
google 文档:
- public static final int START_NOT_STICKY
- Added in API level 5
- Constant 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_INTENT
- Added in API level 5
- Constant 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_STICKY
- Added in API level 5
- Constant 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)
0 0
- android Service onStartCommand intent为nullhttp://hold-on.iteye.com/blog/2024926
- Android中service的onStartCommand()方法中intent为null的问题
- android中service的onStartCommand()方法中intent为null的问题
- 我的Android进阶之旅------>android中service的onStartCommand()方法中intent为null的问题
- Android Service onStartCommand()介绍
- Android Service onStartCommand 理解
- Android 应用程序目录结构http://lovezhou.iteye.com/blog/835155
- android ImageView scaleType属性http://jameszhao84.iteye.com/blog/1397611
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- Android中Service类onStartCommand
- 4.3.4 磁盘组织与管理
- sublime 调用 shell
- 第6章7节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-注入按键事件实例
- iOS拨打电话
- 第6章8节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-小结
- android Service onStartCommand intent为nullhttp://hold-on.iteye.com/blog/2024926
- mysql 创建表时以UTF8编码
- C#的Activator.CreateInstance方法
- git版本控制开发流程小结笔记(一)
- linux SVN服务器创建版本库
- servlet向jsp传数据
- 第8章1节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行环境初始化
- c++ primer plus 第六章变成练习 第7题
- 红黑树