`
hold_on
  • 浏览: 456203 次
  • 性别: 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` 是一个非常重要的组件,它可以在后台运行长时间的操作而不会为用户提供任何界面。`Service` 的生命周期方法之一是 `onStartCommand()`,该方法会在通过 `startService()` 启动服务时被...

    android service 简单实例源代码

    总之,Android Service是实现后台操作的重要工具,通过合理的使用和服务生命周期管理,可以为用户提供高效、稳定的后台服务。而 Binder 机制则让Service具备了跨进程通信的能力,极大地扩展了Service的应用场景。...

    Android Service简单实例

    在Android应用开发中,Service是四大组件之一,用于在后台执行长时间运行的操作,即使用户界面关闭也能继续工作。本篇文章将深入探讨`startService`类型的Android Service,通过一个简单的实例来展示其工作原理和...

    Android Service

    2. **onStartCommand(Intent, int, int)**:每当通过startService()启动Service时,这个方法会被调用。Intent携带了启动Service的请求信息,而返回值用于指示Service如何处理新的启动请求。 3. **onBind(Intent)**...

    Android Service 服务不被杀死的妙招

    如果Service被杀死时没有Intent在处理,那么`onStartCommand()`中的Intent参数将会是null。 2. `START_NOT_STICKY`:如果Service被杀死,系统不会自动重启它。只有当再次通过`startService()`调用时,Service才会...

    Android Service之start实现

    在Android应用开发中,Service是四大组件之一,它在后台执行长时间运行的操作,不与用户交互。本篇文章将深入探讨如何使用`startService()`方法来启动Android服务。 Service的启动方式主要有两种:`startService()`...

    android Service 实现的四个案例

    当系统资源紧张时,Service可能会被杀死,这时需要在`onStartCommand()`返回适当的标志(START_STICKY、START_NOT_STICKY或START_REDELIVER_INTENT)来决定服务如何恢复。 总结,Android Service是实现后台任务的...

    Android service start方式启动

    当你调用`startService()`时,Android系统会启动指定的服务,并执行其`onStartCommand()`方法。此方法是服务的主要入口点,用于处理来自客户端的请求。一旦`onStartCommand()`执行完毕,服务会继续在后台运行,直到...

    android service 例子

    在Android应用开发中,`Service` 是一个非常重要的组件,它允许应用程序在后台长时间运行,即使用户已经离开了应用程序的界面。本示例将深入讲解如何创建和使用Android `Service`,这对于初学者来说是一个很好的起点...

    android service实例

    public int onStartCommand(Intent intent, int flags, int startId) { // 当服务被启动时,此方法会被调用 Log.d("MyService", "Service started"); // 在这里执行你的后台任务 return START_STICKY; // 表示...

    android service使用详解

    在Android开发中,Service是四大组件之一,它用于在后台执行长时间运行的操作,即使用户界面已经关闭。Service并不提供用户界面,而是专注于处理任务、播放音乐、与其他应用交互等。本篇文章将深入探讨如何在Android...

    android service基本用法

    本教程将深入探讨Android Service的基本用法,通过一个名为"ServiceDemo"的示例项目来阐述关键概念。 首先,创建一个新的Android工程并添加一个Service。在AndroidManifest.xml文件中声明Service,例如: ```xml ...

    android service的小例子

    在Android开发中,Service是四大组件之一,它用于在后台执行长时间运行的操作,即使用户界面不在前台。Service组件常用于处理音乐播放、定时任务、网络通信等需要长时间运行的任务。本示例“android service的小例子...

    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

    例如,你可以创建一个名为`ExampleService`的类,并在其中实现Service所需的方法。 ```java public class ExampleService extends Service { @Override public IBinder onBind(Intent intent) { return null; }...

    MyApplication_androidservice_android_363ab.con_

    本示例"MyApplication_androidservice_android_363ab.con"着重展示了如何在Android应用中实现在网络断开时发送通知的功能,这通常对提升用户体验和增强应用功能至关重要。 首先,我们来了解`BroadcastReceiver`。...

    Android 四大组件之Service的Demo

    如果Service已经存在,onCreate()只会执行一次,而onStartCommand()每次都会调用,用于传递新的Intent数据。 2. bindService():这种方式主要用于与Service进行交互,提供客户端-服务器模式的通信。当客户端通过...

    Android 开发 Service讲解.docx

    如果 Service 还没有运行,则 Android 先调用 onCreate() 然后调用 onStartCommand();如果 Service 已经运行,则只调用 onStartCommand(),所以一个 Service 的 onStartCommand 方法可能会重复调用多次。 与 ...

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

    在Android系统中,Service是应用程序组件之一,它可以在后台长时间运行,即使用户界面不在前台。Service主要用于执行长时间运行的操作,例如播放音乐、处理网络事务或与远程服务交互。本篇文章将详细阐述如何创建和...

    android如何绑定service

    在Android开发中,Service是应用程序组件之一,它用于在后台执行长时间运行的操作,即使用户界面不在前台。在本文中,我们将深入探讨如何在Android应用中绑定Service,这通常用于实现客户端-服务器通信,使得应用...

Global site tag (gtag.js) - Google Analytics