关于 sendBroadcastAsUser 的唯一文档是这样说的:
sendBroadcastAsUser(Intent intent, UserHandle user, String receiverPermission)
Version of sendBroadcast(Intent, String) that allows you to specify the user the broadcast will be sent to.
但我一直无法找到有关如何指定用户或如何设置监听器来收听此类广播的任何示例。
在 AOSP 源代码中,我找到了这个例子:
Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
intent.putExtra(PhoneConstants.STATE_KEY,
DefaultPhoneNotifier.convertCallState(state).toString());
if (!TextUtils.isEmpty(incomingNumber)) {
intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
}
intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
android.Manifest.permission.READ_PHONE_STATE);
UserHandle.ALL 作为指定用户意味着什么,如果它的字面意思是“全部”,那么将 sendBroadcastAsUser() 与 ALL 一起使用而不是仅使用普通的 sendBroadcast() 有什么意义?
我的应用能否收听这个特定的系统广播?
最佳答案
sendBroadcast()
- 仅向当前用户中运行的应用程序广播 Intent
sendBroadcastAsUser
允许向在其他应用程序中运行的应用程序发送广播。但这需要 INTERACT_ACROSS_USERS_FULL
权限,而该权限不适用于第 3 方应用程序。是这个原因,api没有文档说明
what is the point in using sendBroadcastAsUser() with ALL rather than just use the vanilla sendBroadcast()?
sendBroadcastAsUser 允许向特定用户发送广播,或者在使用 UserHandle.ALL 的情况下向所有用户发送广播。
Would my app be able to listen to this particular system broadcast?
是的,只要您的应用有权接收该广播,发送给运行您的应用的用户的任何广播都将能够收听该广播
关于android - sendBroadcastAsUser 的任何进一步文档/教程/示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28226473/