Android的意图过滤器:恒定或String.xml更好 [英] Android intent Filters: better as constant or in String.xml

查看:165
本文介绍了Android的意图过滤器:恒定或String.xml更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个Android项目。我有一个等待接收意图检索从远程服务器的一些信息服务类。我这个服务看起来是这样的:

I am working on my first Android Project. I have a Service class that waits for receive intents to retrieve some information from a remote server. This service of mine looks something like this:

public class MyService extends IntentService{
    public static final String ACTION_INTENT_01 = "xyz.actionIntent01";
    public static final String ACTION_INTENT_02 = "xyz.actionIntent02";
    public static final String ACTION_INTENT_03 = "xyz.actionIntent03";
    ... A lot of constant more...
   public static final String ACTION_INTENT_01_EXTRA = "xyz.actionIntent01Extra";
   ...more and more constants...

   public MyService(){
      ...
   }
   /*The Rest of The Service*/

}

我的问题是,什么是更好的,有这个类里面有很多常量字符串或者宣布在String.xml文件,并通过的getString(R.string.ACTION_INTENT_03)访问它们 ??

谢谢!

推荐答案

这些无。我建议有一个常量类,并把这些常量在类。这样,您的服务类是体积更小,更易于维护。

None of these. I would recommend to have a constants class and put these constants in that class. In this way your service class is smaller and easier to maintain.

您应该把字符串中的XML文件的字符串只有在这些受本地化的变化:在英语中有一个特定的值,在法国不同的值等。或者,如果你想在XML UI的布局使用它们。所以,如果你有常量只有在code使用的,没有理由让他们在XML字符串。

You should put strings in strings XML files only if those are subject for localization changes: in English have a specific value, in French a different value and so on. Or if you want to use them in XML UI layouts. So if you have constants to use only in code, there's no reason to have them in XML strings.

这篇关于Android的意图过滤器:恒定或String.xml更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆