androidandroid-intentintentfilter

How to match an intent without data with an intentFilter with dataSchemes?


When you add a data scheme to an intent filter in Android, an intent will match only if it shares the same scheme. If you don't specify any scheme, an intent will match only if it has no data. Now, if my intent filter has a scheme, how can I make it match with an intent that has no data ?

In my case, I want to detect when the wallpaper changes with ACTION_WALLPAPER_CHANGED (I know it is deprecated but I have no other way and it still works). My intent filter has one data scheme ("package", because I also listen for package changes) and since ACTION_WALLPAPER_CHANGED has no data in it, it doesn't match with my intent filter. I could remove the scheme to catch the wallpaper change but then I could not listen for package changes. How can I listen for both package changes with my data scheme and listen for wallpaper change ?

Here is my code to register my custom broadcast receiver (actionReceiver):

actionReceiver = new ActionReceiver();
IntentFilter actionFilter = new IntentFilter();
actionFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
actionFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
actionFilter.addDataScheme("package");
ContextCompat.registerReceiver(this, actionReceiver, actionFilter, ContextCompat.RECEIVER_EXPORTED);

Solution

  • You can have as many Intent filters as you want. Just add another Intent filter. Example (in code):

    IntentFilter filter1 = new Intent(Intent.ACTION_WALLPAPER_CHANGED);
    IntentFilter filter2 = new Intent(Intent.ACTION_PACKAGE_CHANGED);
    filter2.setDataScheme(...)
    
    BroadcastReceiver myReceiver = new MyReceiver();
    context.registerReceiver(myReceiver, filter1);
    context.registerReceiver(myReceiver, filter2);