`
hold_on
  • 浏览: 451399 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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中onStartCommand返回值.txt

    android Service中onStartCommand返回值.txt 有四种返回值,分别代表的含义

    对Service中onStartCommand方法返回值的探索 源码

    “对Service中onStartCommand方法返回值的探索”一文源码 博客地址:http://blog.csdn.net/u012975705/article/details/49932783

    android service demo

    public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub Log.e(TAG, " onStartCommand "); return super.onStartCommand(intent, flags, startId); } }

    Android Service 服务不被杀死的妙招

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理。 从Android官方文档中,我们知道onStartCommand有...

    Android实现双进程守护,如何保证Service不被Kill

    Android实现双进程守护,如何保证Service不被Kill,onStartCommand方法,返回START_STICKY,手动返回START_STICKY,亲测当service因内存不足被kill,当内存又有的时候,service又被重新创建,比较不错,但是不能保证...

    Android编程实现开始及停止service的方法

    你可以从一个activity或从其它应用的组件通过传递一个Intent(指定了要启动的服务)给startService()启动一个服务.Android系统然后调用service的onStartCommand()方法并且把Intent传递给它.(你永远不能直接调用...

    【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 ).zip

    【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 | Service 组件 onStartCommand 方法分析 | 源码资源 ) https://hanshuliang.blog.csdn.net/article/details/115548051 博客源码快照

    Android代码-ServiceKeep

    Android Service保活模块(5.0以下使用c来fork进程,5.0以上使用JobScheduler来保活进程) 具体调用: 1、在WorkService的onStartCommand中执行要保活的操作业务。 2、在初始化过程中调用KeepAliveManager.INSTANCE....

    Android 如何保证service在后台不被kill

    Android 其实无法做多绝对的不被后台kill掉,我们只能尽量使用一些操作提升不被kill的机会。...如果没有传递任何开始命令给service,那将获取到null的intent。  START_NOT_STICKY 在运行onStartCom

    Android创建服务之started service详细介绍

     应用组件(例如Activity)调用startService()来启动一个Service,将需要的参数通过Intent传给Service,Service将会在onStartCommand函数中获得Intent。 有两种方式可以创建started service,一种是扩展Service类,...

    android后台运行的service服务的创建

    安卓的service的创建和调用其实和activity基本上一个样,很简单,只是继承Service类,在里面写一个onStartCommand方法,然后在该方法下写自己的代码就可以了。

    Android实验五.doc

    计算机科学与技术系 实 验 报 告 专业名称 计算机科学与技术 课程名称 Android嵌入式软件开发 项目名称 Service 后台服务 班 级 计科一班 学 号 姓 名 同组人员 无 实验日期 2016.10.11 一、实验目的与要求: 【实验...

    精通ANDROID 3(中文版)1/2

    1.5.5 Android Service组件  1.5.6 Android媒体和电话组件  1.5.7 Android Java包  1.6 利用Android源代码  1.7 本书的示例项目  1.8 小结  第2章 设置开发环境  2.1 设置环境  2.1.1 下载JDK 6  ...

    androidReview:青岛理工大学15级安卓复习知识点

    特点没有用户界面,其他特性很像Activity比后台Activity优先级高,不会被轻易终止即使service被终止,当系统资源恢复的时候,也将自动恢复运行状态,(onStartCommand 返回 START_STICKY)用于进程之间通信,解决两...

    AlarmManager + Broadcast 循环启动任务

    1. 通过 AlarmManager 来设置定制任务 2. 用广播去启动Service 3. Service中onStartCommand()方法执行任务

    详解Android Service 使用时的注意事项

    每一次调用 startService 都会回调onStartCommand,之后调用了stopService之后就会 destroy Service。即使有多个client启动服务,那调用一次stopService 就能 destroy Service 。通过这种方式还有一个好处就是...

    详解Android中IntentService的使用方法

    Android中的IntentService是继承自Service类的,在我们讨论IntentService之前,我们先想一下Service的特点: Service的回调方法(onCreate、onStartCommand、onBind、onDestroy)都是运行在主线程中的。当我们通过start...

    安卓应用应用程序设计试题C.docx

    Android 关于启动方式的service生命周期的onCreate()和onStartCommand()方法,说法正确的是( D ) A、当第一次启动的时候只会调用onCreate()方法 B、当第一次启动的时候只会调用onStartCommand() 方法 C、如果...

    精通Android 3 (中文版)2/2

    1.5.5 Android Service组件  1.5.6 Android媒体和电话组件  1.5.7 Android Java包  1.6 利用Android源代码  1.7 本书的示例项目  1.8 小结  第2章 设置开发环境  2.1 设置环境  2.1.1 下载JDK 6  ...

    Android8.0适配前台定位服务service的示例代码

    从Android 8.0开始系统为实现降低功耗,对后台应用获取用户位置信息频率进行了限制,每小时只允许更新几次位置信息,详细信息请参考官方说明。按照官方指引,如果要提高位置更新频率,需要后台应用提供一个前台服务...

Global site tag (gtag.js) - Google Analytics