Android 即使我强制停止应用程序,whatsapp 服务如何重新启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11237064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:27:00  来源:igfitidea点击:

how does whatsapp service gets restarted even if i force stop app?

androidservicebackgroundwhatsapp

提问by cesarmax

I am running whatsapp (we could call it appX from now on) in device A. I go to manage applications -> force close so appX gets closed and i no longer see appX as running services.

我正在设备 A 中运行 whatsapp(从现在开始我们可以称之为 appX)。我去管理应用程序 -> 强制关闭所以 appX 被关闭,我不再将 appX 视为正在运行的服务。

Now, after 5 minutes, I send a message from another device 's appX (device B) to device A appX (the one we killed it). Here are the 2 scenarios i tested :

现在,5 分钟后,我从另一台设备的 appX(设备 B)向设备 A appX(我们杀死它的那个)发送了一条消息。这是我测试的 2 个场景:

  • device A with android 2.1 : it never receives the message, therefore we could say that none of appX services got restarted. It ONLY receives the message if manually the user restarts the app.

  • device A with android 2.3.6 : for SOME magic reason, no matter how long it's been since appX got killed, as soon as we send the message from device B -> device A gets the message, therefore, appX's service gets restarted. Note : all the time that appX was closed and WITHOUT receiving any notification, i wasn't able to see any running services of appX in manage applications, so this means that this magic service gets restarted as soon as it receives a message/notification

  • 带有 android 2.1 的设备 A:它永远不会收到消息,因此我们可以说没有任何 appX 服务重新启动。如果用户手动重新启动应用程序,它只会收到消息。

  • 带有 android 2.3.6 的设备 A:出于某种神奇的原因,无论 appX 被杀死已有多久,只要我们从设备 B 发送消息 -> 设备 A 收到消息,因此,appX 的服务就会重新启动。注意:一直关闭 appX 并且没有收到任何通知,我无法在管理应用程序中看到任何正在运行的 appX 服务,因此这意味着一旦收到消息/通知,这个神奇的服务就会重新启动

I know it sounds weird, and lot of people will say this is impossible, but again, this has been tested on these 2 devices.

我知道这听起来很奇怪,很多人会说这是不可能的,但同样,这已经在这两个设备上进行了测试。

I am trying to accomplish this same behavior, so any help will be appreciated it.

我正在尝试完成相同的行为,因此将不胜感激。

回答by herom

Before you up/down-vote this answer please take into account that the answer is nearly 3 years old now (speaking of May 2015) and things may have changed!

在您对这个答案投赞成票/反对票之前,请考虑到这个答案现在已经将近 3 年了(说到 2015 年 5 月),而且情况可能已经发生了变化!

I don't think it's some magic what happens here! It's just Android C2DM (see: https://developers.google.com/android/c2dm/), whereas the app has a registered Receiverfor incoming Push Notifications and gets awaken by this message. Android C2DM is/was available with Android 2.2, that's the reason why you can't see the same behaviour on your device with Android 2.1 up and running.

我不认为这里发生的事情有什么神奇之处!它只是 Android C2DM(请参阅:https: //developers.google.com/android/c2dm/),而该应用程序注册Receiver了传入的推送通知并被此消息唤醒。Android C2DM 可用于 Android 2.2,这就是为什么您在运行 Android 2.1 的设备上看不到相同行为的原因。

By the way: As you can see, C2DM is deprecated since June 26th, 2012. So instead of C2DM, one should use GCM (see: http://developer.android.com/guide/google/gcm/gs.html)

顺便说一句:如您所见,自 2012 年 6 月 26 日起,C2DM 已被弃用。因此,不应使用 C2DM,而应使用 GCM(请参阅:http: //developer.android.com/guide/google/gcm/gs.html

Useful Comment: GCM needs available internet connection. You can using any other broadcast receiver such as SMSReceiver for by passing this limitation.

有用的评论:GCM 需要可用的互联网连接。通过传递此限制,您可以使用任何其他广播接收器,例如 SMSReceiver。

回答by bonnyz

Starting from Android 3.1(API 12), if an application is force-stopped it will not restart until the user manually runs the app again.

Android 3.1(API 12) 开始,如果应用程序被强制停止,它将不会重新启动,直到用户再次手动运行该应用程序

This will happen even if the app contains a Serviceor an active BroadcastReceiver.

即使应用程序包含一个Service或一个活动的BroadcastReceiver.

You can find the official documentation here.

您可以在此处找到官方文档。

回答by Lokesh

i dont have idea about whatsApp service.

我不知道 WhatsApp 服务。

But it is possible that after force stop application, restart service of app.

但是有可能在强制停止应用程序后,重新启动应用程序的服务。

i use START_STICKYservice for my chatApp. i have to do same thing so i use START_STICKY service so when my app kill or force stop from setting, after few second my service get restart and i able to login to my xmpp server and get incoming message.

我的聊天应用程序使用START_STICKY服务。我必须做同样的事情,所以我使用 START_STICKY 服务,所以当我的应用程序终止或强制停止设置时,几秒钟后我的服务重新启动,我能够登录到我的 xmpp 服务器并接收传入消息。

回答by Pramod Yadav

its nothing magical here appX uses push notifications via android GCM platform https://developer.android.com/google/gcm/index.htmlin GCM the app registers for a braodcast reciever and the broadcast receiver starts the service on getting the push notification. Android GCM is dependent on google play services that are available on android 2.2 and beyond that therefore you didn't see the message on 2.1 device

它没有什么神奇之处 appX 通过 Android GCM 平台https://developer.android.com/google/gcm/index.html在 GCM 中使用推送通知,应用程序注册广播接收器,广播接收器在获取推送通知时启动服务. Android GCM 依赖于可在 android 2.2 及更高版本上使用的 google play 服务,因此您在 2.1 设备上看不到该消息