手机启动后,后台broadcast满天飞。可能点击自己应用的Notification 启动的broadcast半天不响应。这时候加上FLAG_RECEIVER_FOREGROUND吧。注意此flag在API 16中引入.
示例:
注FLAG_RECEIVER_FOREGROUND的doc:
示例:
Intent intent = new Intent(context, XXXReceiver.class);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
注FLAG_RECEIVER_FOREGROUND的doc:
/**
* 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.
*/
public static final int FLAG_RECEIVER_FOREGROUND = 0x10000000;
本文介绍了一个技巧,通过在Android应用程序中使用FLAG_RECEIVER_FOREGROUND标志来提高Broadcast接收者的响应速度。此标志允许Broadcast接收者在前台优先级下运行,从而缩短了等待时间。适用于API 16及以上版本。
943

被折叠的 条评论
为什么被折叠?



