Android - 无法启用BroadcastReceiver

发布于 2024-08-17 10:42:11 字数 1287 浏览 6 评论 0原文

升级到最新的 SDK 后,我回到小部件开发,突然我的小部件在启动时失败并显示以下消息:

ERROR/AndroidRuntime(5296): java.lang.RuntimeException: 
Unable to start receiver topjob.widget.SearchWidget: 
java.lang.SecurityException: Permission Denial: 
attempt to change component state from pid=5296, uid=10057, package uid=10048

这是发生异常的两行代码:

@Override
public void onEnabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName("topjob",
            ".widget.SearchWidgetBroadcastReceiver"), 
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    startAlarm(context, UPDATE_RATE_SEC);
}

所以在上面的代码中 startAlarm() 从未执行,因为 pm.setComponentEnabledSetting() 抛出 SecurityException

我是否在清单中缺少任何安全设置?目前我有:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

我正在开发 SDK v. 1.5 并且它曾经工作得

很好它发生在手机和模拟器上

I got back to the widget development after upgrading to the latest SDK and all of the sudden my widget is failing on startup with this message:

ERROR/AndroidRuntime(5296): java.lang.RuntimeException: 
Unable to start receiver topjob.widget.SearchWidget: 
java.lang.SecurityException: Permission Denial: 
attempt to change component state from pid=5296, uid=10057, package uid=10048

Here's two lines of code where exception occurs:

@Override
public void onEnabled(Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName("topjob",
            ".widget.SearchWidgetBroadcastReceiver"), 
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    startAlarm(context, UPDATE_RATE_SEC);
}

so in the code above startAlarm() is never executed since pm.setComponentEnabledSetting() throws the SecurityException

Am I missing any security settings in my manifest? Currently I have:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

I'm developing for SDK v. 1.5 and it used to work fine

P.S. It happens on the phone and the emulator

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一人独醉 2024-08-24 10:42:11

在manifest.xml中您将添加

<permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
android:sharedUserId="android.uid.system"

在Android.mk中将添加

LOCAL_CERTIFICATE := platform  

这就是全部。

At the manifest.xml you will add

<permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
android:sharedUserId="android.uid.system"

At the Android.mk will add

LOCAL_CERTIFICATE := platform  

this is all.

蓝天 2024-08-24 10:42:11

您可以尝试在清单中添加此权限吗?

<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>

Can you try adding this permission in your manifest?

<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
2024-08-24 10:42:11

好的 - 这不是真正的答案,而是一种解决方法。如果有人想介入并提供关于如何完成此操作的答案或解释,我会很乐意接受。
无论如何 - 我通过将 newState 标志从 COMPONENT_ENABLED_STATE_ENABLED 更改为 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT 来使其工作。由于我的广播接收器已在清单中启用状态,因此它工作得很好

OK - this is not really answer but rather a workaround. If someone wants to step in and provide answer or explanations on how this should be done I would gladly accept that.
Anyway - I got it working by changing newState flag from COMPONENT_ENABLED_STATE_ENABLED to PackageManager.COMPONENT_ENABLED_STATE_DEFAULT. Since my broadcast receiver has enabled state in the manifest it works just fine

尐偏执 2024-08-24 10:42:11

当我尝试禁用正在运行的包中的某些组件时,我只收到此错误“java.lang.SecurityException:权限拒绝:”。否则,我会成功。

I only got this error "java.lang.SecurityException: Permission Denial: " when I try to disable some component out of the package it is running in.Otherwise, I got success.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文