简体   繁体   English

对于GcmListenerService是onMessageReceived后台吗?

[英]For GcmListenerService is onMessageReceived background?

When I extend GcmListenerService , is onMessageReceived running in a background process or is it on the UI thread? 当我扩展GcmListenerServiceonMessageReceived是在后台进程中运行还是在UI线程上运行? I need to know if I may make network calls inside the method body without using an asyncTask or such. 我需要知道是否可以在方法体内进行网络调用而不使用asyncTask等。

I looked at the source code and sure enough it is a vanilla service with no handlers. 我查看了源代码,果然它是一个没有处理程序的vanilla服务。 But there is a bit of code about THREAD_POOL_EXECUTOR and a number of apparently obfuscated stuff. 但是有一些关于THREAD_POOL_EXECUTOR的代码和许多明显混淆的东西。

When I extend GcmListenerService , is onMessageReceived running in a background process or is it on the UI thread? 当我扩展GcmListenerServiceonMessageReceived是在后台进程中运行还是在UI线程上运行? I need to know if I may make network calls inside the method body without using an asyncTask or such. 我需要知道是否可以在方法体内进行网络调用而不使用asyncTask等。

Yes you can make network calls inside onMessageReceived() method because it is running in a background process. 是的,您可以在onMessageReceived()方法内进行网络调用,因为它在后台进程中运行。 For example, you can download an image in this method before displaying it on notification using big image notification style. 例如,您可以使用大图像通知样式在通知中显示此方法之前下载图像。

Simple check to determine whether current thread is main UI thread 简单检查以确定当前线程是否是主UI线程

boolean isMain = Looper.getMainLooper().getThread() == Thread.currentThread();

placed inside onMessageReceived method indicates that it is running in background process ( answer to the similar question). 置于onMessageReceived方法内部表示它正在后台进程中运行( 回答类似问题)。

Services aren't running on the Ui / main thread. 服务未在Ui /主线程上运行。

Service can also run (in the background) even when the app is closed, this way you can get push notification, listen to the intent invoked when receiving the push and use it to wake your app. 即使应用程序关闭,服务也可以(在后台)运行,这样您就可以获得推送通知,听取接收推送时调用的意图并使用它来唤醒您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM