android - sendBroadcastAsUser 的任何进一步文档/教程/示例?

标签 android android-intent android-broadcast

关于 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/

相关文章:

java - 如何从android studio中的另一个模块导入类?

android - 使用 System.getProperty ("os.arch") 检查它是否是 armeabi cpu

java - BroadCastReceiver没有接收到数据?

android - 在应用程序关闭后启动带有警报的服务

android - 如何在 "Completing action using"菜单中显示应用程序名称?

android - 与 Android 应用程序中的 Activity 进行通信的独立线程后台服务

android - 如何在android中创建带按钮的半椭圆形页脚

android - 如何在不删除以前版本生成的数据的情况下安装 AIR android 应用程序

android - 如何获取所有设备的图像路径Uri和Crop

Android 短信发送报告 Intent