android Service onStartCommand intent为null

阅读更多

 

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让系统重发intentservice以便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)

 

 

你可能感兴趣的:(android,service,exception,onstartcommand,intent,为,null)