Bostone
Bostone

Reputation: 37126

Android - can't enable BroadcastReceiver

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

Upvotes: 3

Views: 3752

Answers (4)

fengbenpao
fengbenpao

Reputation: 31

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.

Upvotes: 3

basv
basv

Reputation: 455

Can you try adding this permission in your manifest?

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

Upvotes: 1

jiangyan.lily
jiangyan.lily

Reputation: 952

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.

Upvotes: 0

Bostone
Bostone

Reputation: 37126

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

Upvotes: 0

Related Questions