[英]When exactly onReceive from BroadcastReceiver is called?
I know that onReceive
from BroadcastReceiver
is called on the main thread (UI). 我知道在主线程(UI)上调用了
BroadcastReceiver
onReceive
。 Does it mean that my all methods like onStart
, onDestroy
, onClick()
etc have to be finished before onReceive
will be called? 这是否意味着我的所有方法(如
onStart
, onDestroy
, onClick()
等)都必须在调用onReceive
之前完成? Or can onReceive
be called while I'm for example in the middle of onStart
method, like this: 或者可以在例如
onStart
方法中间调用onReceive
,如下所示:
@Override
protected void onStart() {
if (networkChangeReceiver != null) {
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(networkChangeReceiver, intentFilter);
}
method1();
//here connection changes -> can onReceive be called here?
method2();
method3();
(...)
}
Even if onReceive it's called in UI thread don't expect it to appear only after you finish your stuff in onStart/onCreate or whatever. 即使onReceive在UI线程中被调用,也不要期望它仅在您完成onStart / onCreate之类的操作后才会出现。 The broadcast can receive at anytime, most likely somewhere in between or your logic, so your assumtion is correct: can be called right in the middle of your code from onStart() for example.
广播可以随时接收,很可能介于两者之间或您的逻辑之间,因此您的假设是正确的:例如,可以从onStart()的代码中间直接调用该广播。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.