Search code examples
android-intentbroadcastreceiverbroadcastintentfilter

Register CATEGORY_HOME intent on runtime


I need to register (on runtime) custom BroadcastReceiver for intent filter that can be described in manifest as

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.HOME" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

and unregister receiver when user exits application by pressing some button in application (or
entering password).

Receiver registered using code

receiver_ = new MyReceiver();
filter_ = new IntentFilter(Intent.ACTION_MAIN);
filter_.addCategory(Intent.CATEGORY_HOME);
filter_.addCategory(Intent.CATEGORY_DEFAULT);
registerReceiver(receiver_, filter_);

doesn't receve intent, onReceive() function is not called.

What I'm doing wrong and is there any possibilities to solve this promlem? Thanx.


Solution

  • A BroadcastReceiver only gets triggered by a broadcast Intent (ie: an Intent sent using sendBroadcast()). The Intent you are describing (ACTION=MAIN, CATEGORY=HOME) is not a broadcast Intent. That is an Intent that is used to start an Activity (ie: someone calls startActivity() with an Intent that looks like that).

    It seems to me that you are trying to create a HOME-screen replacement. If that is what you want to do, you need to have an Activity with that <intent-filter> in your manifest. You can't register/unregister this dynamically.

    If you need to enable/disable your HOME-Screen replacement, what might work (I haven't tried it, so I can't say for sure that it will work) is to enable/disable your HOME-Screen activity using the PackageManager. Have a look at setComponentEnabledSetting() here: http://developer.android.com/reference/android/content/pm/PackageManager.html

    Let us know what you find out!