广播被延迟 [英] Broadcasts are delayed
问题描述
我们使用广播来传达远程服务和UI之间的状态更改。这样做时,我们发现了一个非常奇怪的行为:有时(我找不到任何线索,为什么)这些广播会延迟8秒左右。
We use broadcasts to communicate state changes between a remote services and our UI. Doing this, we discovered a very strange behaviour: Sometimes (I can not find any clues why) these broadcasts are delayed around 8s.
我们如何发送它们(非常基本, mState
只是一个枚举)(服务中的远程过程):
How we send them (pretty basic, mState
is just a enum) (Remote process in service):
Intent intent = new Intent();
intent.setAction(ACTION_STATE_CHANGED);
intent.putExtra(EXTRA_STATE, mState);
Service.get().sendBroadcast(intent, null);
如何注册静态接收器(应用程序):
How the static receiver is registered (App):
<receiver android:name=".ServiceStateReceiver">
<intent-filter>
<action android:name="service.intent.action.STATE_CHANGE" />
</intent-filter>
</receiver>
接收器类别(应用程序):
The receiver class (App):
public class ServiceStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("State", "State via static received");
}
}
现在有时会延迟(对于相同的州总是如此) )
This is now sometimes delayed (always for the same states)
状态枚举:
public enum State {
DISCONNECTED,
BT_DISABLED,
BT_SCANNING,
BT_TIMEOUT,
BT_FAILURE,
BT_LOCATION_NEEDED,
CONNECTING,
ACTIVATION_FAILURE,
VIN_NEEDED,
CAR_MODEL_NEEDED,
MILEAGE_NEEDED,
READY,
IGNITION_OFF,
IGNITION_ON;
@Override
public String toString() {
return name();
}
}
现在出现了一个奇怪的部分:如果我注册动态接收器,我们总是在那里立即收到所有广播。静态的仍然有那么大的延迟。如果我通过 sendOrderedBroadcast
发送广播(静态和动态)都具有此延迟。
Now comes the strange part: If I register a dynamic receiver we always receive ALL broadcasts immediately there. The static one still has that huge delay. If I send the broadcast via sendOrderedBroadcast
BOTH (static & dynamic) have this delay.
动态接收器:
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("State", "State via dynamic received");
}
}, new IntentFilter(State.ACTION_STATE_CHANGED));
到目前为止我一直在尝试:
What I tried so far:
- 从主线程/工作线程发送广播(未更改)
- 使用权限属性播放(未更改)
- 连续发送广播多次(不更改任何内容,现在仅获得多个延迟的广播)
也:无输出从logcat似乎相关。在不同的设备(OnePlus 3 7.1.1,Z3 6.0.1,S7 Edge 7.1.1)上进行了尝试,它们都表现出相同的行为
Also: No output from logcat which seems related. Tried on different devices (OnePlus 3 7.1.1, Z3 6.0.1, S7 Edge 7.1.1), all show the same behaviour
我认为这可能与以下方面有关: Android网络状态更改检测需要时间
I think this may be related: Android network state change detection takes time
推荐答案
搜索了几个小时的答案后,我发现了?发布此后的解决方案。
After searching for a answer for hours, I found the? solution after posting this.
在意图上添加 FLAG_RECEIVER_FOREGROUND
标志似乎可以完全消除此延迟。仍然很高兴知道为什么会发生这种情况,并且如果这是一个很好的解决方案,或者我是否以此破坏了其他东西。
It seems like that adding the FLAG_RECEIVER_FOREGROUND
flag to the intent completly removes this delay. Would be still nice to know why this happens and if this is a good "fix" or if I destroy something else with this.
这可以解决问题:
intent.setFlags(FLAG_RECEIVER_FOREGROUND);
如果已设置,则在发送广播时允许接收方在
个前台优先级,超时间隔更短。在正常的
广播期间,不会自动将接收者悬挂在
背景优先级之外。
If set, when sending a broadcast the recipient is allowed to run at foreground priority, with a shorter timeout interval. During normal broadcasts the receivers are not automatically hoisted out of the background priority class.
资料来源:
https://developer.android.com/reference/android/ content / Intent.html#FLAG_RECEIVER_FOREGROUND
这篇关于广播被延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!