原因是,通过意向是空的onStartCommand意向、原因、onStartCommand

2023-09-12 09:32:24 作者:拿红颜乱浮生

是否有其他原因,传递给 onStartCommand的意图(意图,INT,INT)将除系统通过一个标志重新启动该服务,如空 START_STICKY

Is there any other reason that the Intent that is passed to onStartCommand(Intent, int, int) would be NULL besides the system restarting the service via a flag such as START_STICKY?

此外,当服务由系统重新启动 Intent.getAction()方法返回NULL ...有时。意图不为NULL只是的getAction()

Also, when the service is restarted by the system the Intent.getAction() method returns NULL... sometimes. Intent is not NULL just getAction()

我问这里太多,但都没有得到答复只是还没有。

I asked here too but haven't received an answer just yet.

更新:马克·墨菲聊天后,他建议我返回 START_REDELIVER_INTENT onStartCommand()回调在我的服务,而不是 START_STICKY ,使得整个意图是在重新启动后发送。

UPDATE: After chatting with Mark Murphy, he suggested that I return START_REDELIVER_INTENT in the onStartCommand() callback in my service instead of START_STICKY so that the entire intent is sent following a restart.

我不这样做最初是因为我担心的是,如果该服务试图做一些事情,那么在考研,重新启动该服务的中间... ...它认识到它开始做的东西?我想这就是我的逻辑将需要负责:)

I didn't do this initially because I was concerned that if the service was attempting to do something, then in the middle of that something the service was restarted... will it recognize that it started doing that something? I guess that is logic I will need to be responsible for :)

推荐答案

我很惊讶有进入的标志没有讨论。我将用下面的监控这在日志中:

I'm surprised there's no discussion of the incoming flags. I'm going to monitor this in the logs with the following:

if (null == intent || null == intent.getAction ()) {
        String source = null == intent ? "intent" : "action";
        Log.e (TAG, source + " was null, flags=" + flags + " bits=" + Integer.toBinaryString (flags));
        return START_STICKY;
}

更新:标志为0,所以没有什么可操作的存在。我已经离开了空检查在那里,没有功能丧失。

Update: Flags were 0 so there was nothing actionable there. I've left the null check in there with no loss of function.

编辑:好吧,我发现它所有的地方START_STICKY的文档! 如果没有任何未决的启动命令都被传递到服务,这将被称为空意图对象,所以你必须要小心检查这个。

Ok, I found it in the documentation of START_STICKY of all places! "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."

http://developer.android.com/reference/android/app/Service.html

 
精彩推荐
图片推荐