我刚刚开始 Android 开发,所以我想要一些关于代码风格的建议。Intent在进行调度的方法中编写调度程序对我来说似乎很好,比如
// 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);
}
}
然后,任何被调用者都只是 run MyService.sendMessage(ctx, arg),而不是Intent在他们的身体里有创建代码。
这似乎是一场胜利:当你想要 eg 时,要记住的东西更少sendMessage,而且你不必同步名称,比如模块"send message"和"my_arg"跨模块。但是,我在谷歌的音乐应用程序中并不经常看到他们开源,所以我想知道是否有缺点,我应该坚持惯例。