Android - can't enable BroadcastReceiver

社会主义新天地 提交于 2019-12-22 10:37:42

问题


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


回答1:


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.




回答2:


Can you try adding this permission in your manifest?

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



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/2052589/android-cant-enable-broadcastreceiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!