Search code examples
androidbroadcastreceiverandroid-package-managers

Enable or disable registered receivers


I have problems providing correct attributes to "setComponentEnabledSetting()" - I get IllegalAttributeException saying that .class can't be find, or doesn't exist.

Receivers are declared in application manifest, and receiver handlers are stored in a separate package (com.app.receivers). In manifest file their name attribute is ".receivers.OnBoot". Till now I didn't have any problems with this setup - receivers are working, etc., but now I am trying to add a feature for user to enable/disable receivers.

ComponentName rec = new ComponentName("com.app.receivers", "OnBoot.class");
getPackageManager().setComponentEnabledSetting(rec, ....);

I have tried many different alternatives to get valid ComponentName, but all failed. How can I properly reference this receiver so that setComponentEnabledSetting() will find it?


Solution

  • just use BroadcastReceiver class name with full package name as second parameter to ComponentName instead of OnBoot.class try it as:

    ComponentName rec = new ComponentName("com.app.receivers",
                                                "com.app.receivers.OnBoot");
    getPackageManager().setComponentEnabledSetting(rec, ....);