本文介绍了广播接收器的onReceive()线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是的onReceive()
的方法广播接收器
线程安全的或者我需要实现我自己的同步?
如果我有的正在的onReceive()
方法中使用的任何类级别的变量,而的onReceive()
方法被多次调用速度非常快,它会造成一个问题?
公共类MyBroadCastReceiver扩展广播接收器{ 布尔isFirstTrigger = TRUE; @覆盖
公共无效的onReceive(上下文的背景下,意图ARG1){
如果(isFirstTrigger)
{
//做一些费时
isFirstTrigger = FALSE;
}
}
解决方案
It will only ever be called on the main application thread. Hence, it is thread-safe with respect to anything else running on the main application thread.
If the BroadcastReceiver
is registered in the manifest, a new instance is created for each broadcast. This is why you do not normally see data members on a BroadcastReceiver
.
这篇关于广播接收器的onReceive()线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!