【问题标题】:using sendBroadcast in a system app在系统应用程序中使用 sendBroadcast
【发布时间】:2013-04-15 11:15:51
【问题描述】:

我无法从作为系统应用程序加载到自定义 rom 中的应用程序发送 Broadcast(在 Manifest 中使用 android:sharedUserId="android.uid.system")。

我遇到的问题是尝试执行简单的 sendBroadcast 时:

Intent newIntent = new Intent(intent.getExtras().getString(BUNDLE_ACTION_TO_REPLY_ON));
newIntent.putExtra(BUNDLE_FILE_URI, bitmapFile.getAbsolutePath());
newIntent.putExtra(BUNDLE_REPLY_WIDTH, width);
newIntent.putExtra(BUNDLE_REPLY_HEIGHT, height);
newIntent.putExtra(BUNDLE_REPLY_EXTRA, extra);
context.sendBroadcast(newIntent);

我在 Logcat 中收到此警告:

Calling a method in the system process without a qualified user

这是由ContextImpl.javawarnIfCallingFromSystemProcess() 进程中抽出的。

有人知道原因吗(如果我需要“修复”它)?

【问题讨论】:

  • 你能说明你在哪里初始化上下文吗?
  • 它作为广播接收器的一部分传入 (public void onReceive(final Context context, final Intent intent))
  • 这个 BroadcastReceiver 是你的系统应用吗?
  • 应用中有一个 BroadcastReceiver 是的 :)
  • 不幸的是,该项目已从我的优先级列表中移出,因此我没有机会尝试@sMiLo 的答案,这看起来是个不错的选择。

标签: android


【解决方案1】:

使用下面的函数代替sendBroadcast(Intent intent)

void sendBroadcastAsUser(Intent intent, UserHandle user)

例如:

context.sendBroadcastAsUser(newIntent, new UserHandle(UserHandle.USER_CURRENT));

【讨论】:

  • 非预装应用无法使用,请阅读:link
  • @Mat:是的。它仅适用于系统应用程序。
  • UserHandle.USER_CURRENT 参数是什么?
  • Process.myUserHandle() 而不是 new UserHandle(UserHandle.USER_CURRENT) 为我完成了这项工作
  • 您不需要新的 UserHandle 对象,只需使用 UserHandle.CURRENT
猜你喜欢
  • 2012-02-14
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2013-11-15
  • 1970-01-01
  • 2013-06-17
相关资源
最近更新 更多