Search code examples
androidclassintentfilter

IntentFilter as stand alone class


I would like to create my intentFilter as an instance of pre-defined class. But somehow the eclipse throws errors at me when I am trying to do something like this. (this is just rough idea ... i have tried different approaches either)

import android.content.IntentFilter;

public class MyIntentFilter extends IntentFilter {

MyIntentFilter.addAction(INTENT_BY_MAIN); // I am not sure about this line
MyIntentFilter.addAction(INTENT_BY_OTHER);
MyIntentFilter.addAction(INTENT_BY_WHATEVER);

}

the implementation should look like this:

MyIntentFilter mFilter = new MyIntentFilter();
mBroadcastReceiver mReceiver = new mBroadcastReceiver(mHandler);
this.registerReceiver(mReceiver, mFilter); 

P.S.

I want to avoid defining IntentFilter in XML or in Activity itself. The reason? To make the code detached as much as possible, and thus readable (hopefully)


Solution

  • I don't understand why you want to extend IntentFilter. If it's just for avoid XML definition, you can simply do something like that, it should work (and nothing more is added in your manifest) :

    mBroadcastReceiver mReceiver = new BroadcastReceiver(mHandler);
    this.registerReceiver(mReceiver, new IntentFilter("MY_ACTION");
    this.registerReceiver(mReceiver, new IntentFilter("MY_ACTION_2");
    this.registerReceiver(mReceiver, new IntentFilter("MY_ACTION_3");