简体   繁体   English

Android:BroadcastReceiver时间限制

[英]Android: BroadcastReceiver Time limit

是否为BroadcastReceiver.onReceive方法中运行的操作定义了任何时间限制?

onReceive() is called on the main application thread, the same thread that drives your UI. onReceive()在主应用程序线程上调用,该线程驱动您的UI。 In general, you want onReceive() to return in under a millisecond, in case your UI is in the foreground, so you do not freeze the UI (aka, have "jank"). 通常,你希望onReceive()在毫秒内返回,以防你的UI在前台,所以你不要冻结UI(也就是说,有“jank”)。 There is also a 5-10 second limit, after which Android will basically crash your app. 还有5-10秒的限制,之后Android将基本崩溃您的应用程序。

However, you cannot reliably fork a background thread from onReceive() , as once onReceive() returns, your process might be terminated, if you are not in the foreground. 但是,您无法从onReceive()可靠地派生后台线程,因为一旦onReceive()返回,如果您不在前台,您的进程可能会终止。

For a manifest-registered receiver, a typical pattern is to have onReceive() delegate the work to an IntentService , which has its own background thread and, being a service, tells the OS that your process is still doing some work and should let your process run a bit longer. 对于清单注册的接收器,一个典型的模式是让onReceive()将工作委托给一个IntentService ,它有自己的后台线程,作为一个服务,告诉操作系统你的进程仍在做一些工作,应该让你的过程运行一点时间。

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

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