FirebaseMessagingService默认情况下是在后台运行吗?

时间:2016-12-09 18:54:40

标签: android firebase android-service firebase-cloud-messaging android-intentservice

FirebaseMessagingService在后​​台运行与IntentService的运作方式类似吗?

我看到FirebaseMessagingService范围Service不在后台运行,但我想确定我是否应该在FirebaseMessagingService内异步进行任何工作或同步。

谢谢

2 个答案:

答案 0 :(得分:12)

if (choice == 1) { switch (from) { case 0: { if (to == 1) std::cout << " by foot "; if (to == 2) std::cout << " by foot -> by bus "; if (to == 3) std::cout << " by air "; break; } case 1: { if (to == 0) std::cout << " by foot "; if (to == 2) std::cout << " by bus "; if (to == 3) std::cout << " by bus -> by car "; break; } case 2: { if (to == 0) std::cout << " by bus -> by foot "; if (to == 1) std::cout << " by bus "; if (to == 3) std::cout << " by car "; break; } case 3: { if (to == 1) std::cout << " by car -> by bus "; if (to == 2) std::cout << " by car "; if (to == 0) std::cout << " by air "; } } 的方法FirebaseMessagingService在后​​台调用&#34;&#34; (不在UI /主线程上)。如果您尝试在onMessageReceived(RemoteMessage message)内进行异步工作,则会收到错误消息:

  

必须从主线程调用方法execute,当前推断的线程是worker。

所以在onMessageReceived(RemoteMessage message)内完成的所有工作都应该同步进行,因为它在自己的后台工作线程中。

答案 1 :(得分:4)

Service没有&#34;在后台运行&#34;。 Service只是一个类的实例(即:一个对象)。 Service的方法可以在主(UI)线程或后台(工作线程)线程上运行。这一切都取决于他们的召唤方式。

服务onCreate()onStartCommand()onDestroy()的生命周期方法都在主(UI)线程上调用。但是在你的Service中,你可以启动其他线程并在这些线程上做其他事情。