I’m just starting Android development, so I’d like a little advice on code style. It seems nice to me to write Intent dispatchers in methods that are doing the dispatching, like
// in case it's not clear, names are meta-variables
public class MyService...
...
public static void sendMessage(Context ctx, MyArgClass myArg) {
Intent sendIntent = new Intent(ctx, MyService.class);
sendIntent.setAction("send message");
sendIntent.putExtra("my_arg", myArg);
ctx.startService(sendIntent);
}
}
then, any callees just run MyService.sendMessage(ctx, arg), instead of having the Intent creation code in their bodies.
It seems like a win: there’s less stuff to remember when you want to e.g. sendMessage, and you don’t have to synchronize names, like "send message" and "my_arg" across modules. However, I don’t see it that often in Google’s music app that they’ve open sourced, so I’m wondering if there are downsides, and I should stick to convention.
It is good practice. This pattern can be found in android developer guides (sample)