Android - 无法启用BroadcastReceiver
升级到最新的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在manifest.xml中您将添加
在Android.mk中将添加
这就是全部。
At the manifest.xml you will add
At the Android.mk will add
this is all.
您可以尝试在清单中添加此权限吗?
Can you try adding this permission in your manifest?
好的 - 这不是真正的答案,而是一种解决方法。如果有人想介入并提供关于如何完成此操作的答案或解释,我会很乐意接受。
无论如何 - 我通过将
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 fromCOMPONENT_ENABLED_STATE_ENABLED
toPackageManager.COMPONENT_ENABLED_STATE_DEFAULT
. Since my broadcast receiver has enabled state in the manifest it works just fine当我尝试禁用正在运行的包中的某些组件时,我只收到此错误“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.