I want to use switch-case statement, too.
So I create custom enum class that its constructor requires intent action string like below:
(because enum can be used for switch-case statement)
enum ACTION {
INVALID("action.invalid"), // action for ignore.
FOO("action.foo"),
BAR("action.bar"),;
private final String action;
ACTION(String action) {
this.action = action;
}
public String getAction() { // to use it for intentFilter.
return action;
}
public static ACTION getEnum(String action) {
for (ACTION act : values()) {
if (act.action.equals(action)) return act;
}
return INVALID; // do not throw exception like IllegalArgumentException or else. throw exception may disturb your code refactoring.
}
// example - just broadcast
public void broadcast(Context ctx, PARAM.Abstract p) { // I will explain PARAM class later.
Intent intent = new Intent();
intent.setAction(action);
p.injectExtras(intent); // see impl below!
ctx.sendBroadcast(intent);
}
// example - use AlarmManager
public void alarm(Context ctx, Date time, PARAM.Abstract p) {
Intent intent = new Intent();
intent.setAction(action);
p.injectExtras(intent); // see impl below!
AlarmManager alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, 0);
alarmManager.set(AlarmManager.RTC, time.getTime(), pendingIntent);
}
// of course, you can use ctx.startService, or else.
}
use example:
// trigger it.
ACTION.FOO.broadcast(context,new PARAM.Abstract());
...
// receive & handle it.
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
ACTION anEnum = ACTION.getEnum(action);
switch (anEnum) {
case INVALID: // it can be occured by old version's legacy alarm.
Log.w("INVALID action occured:"+action);
break;
case FOO:
// do something for FOO
break;
case BAR:
// do something for FOO
break;
defalut:
Log.d("Unimplement! action:"+action);
break;
}
}
...
// and I recommend below, when register broadcast receiver.
// create this method below of onReceive.
private IntentFilter getIntentFilter() {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION.FOO.getAction());
filter.addAction(ACTION.BAR.getAction());
return filter;
}
...
// register your receiver like this
BroadcastReceiver my = new MyBroadcastReceiver();
registerReceiver(my, my.getIntentFilter());
explain PARMA.Abstrat
Using enum to broadcast is handy.
It reduces work likes typing codes(} else if(action.equals("blahblah") {
) and declaring actions(static final ACTION_FOO="action.foo"
).
but.. when you need to put some extras to send something to other component, you can find out there is no way to do like ACTION.FOO.broadcastWithParam(p);
. even if you implemented broadcastWithParam()
on FOO
you can't access method broadcastWithParam()
. because that is how enum works.(see this thread for detail)
so we need to do it something other way.
In my case, I decide to use class (PARAM
).
declare:
class PARAM {
public static class Abstract {
protected void injectExtras(Intent intent){
// do nothing for action don't need to extas...
}
}
public static class FOO extends Abstract { // this is for ACTION.FOO
public static final String P1_KEY="p1";
public static final String P2_KEY="p2";
String p1;
int p2;
public FOO(String p1, int p2){
this.p1 =p1;
this.p2 =p2;
}
@Override
protected void injectExtras(Intent intent){
intent.putExtra(P1_KEY,this.p1);
intent.putExtra(P2_KEY,this.p2);
}
}
public static class BAR extends Abstract{ // this is for ACTION.BAR
...
}
}
use example:
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
ACTION anEnum = ACTION.getEnum(action);
switch (anEnum) {
case FOO:
Bundle extras = intent.getExtras();
String p1 = extras.getString(PARAM.FOO.P1_KEY);
int p2 = extras.getString(PARAM.FOO.P2_KEY);
// do something with p1 & p2;
break;
}
}
Finally,
I use 'ACTION&
PARAM` as inner class of Service, BroadcastReceiver or else.
example:
public class MyIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
ACTION anEnum = ACTION.getEnum(action);
switch (anEnum) {
case INVALID: // it can be occured by old version's legacy alarm.
Log.w("INVALID action occured:"+action);
break;
case FOO:
// do something for FOO
break;
case BAR:
// do something for FOO
break;
defalut:
Log.d("Unimplement! action:"+action);
break;
}
}
}
public static enum ACTION {...}
public static class PARAM {...}
}