FirebaseMessagingService
在后台运行与IntentService
的运作方式类似吗?
我看到FirebaseMessagingService
范围Service
不在后台运行,但我想确定我是否应该在FirebaseMessagingService
内异步进行任何工作或同步。
谢谢
答案 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
中,你可以启动其他线程并在这些线程上做其他事情。