字符串引用不应该适用于 AndroidManifest 中意图过滤器的操作名称吗?
最初,我的 AndroidManifest.xml 包含一个我通过其自定义操作名称访问的活动。
<activity
android:label="HERE I AM"
android:name="TestController">
<intent-filter>
<action android:name="com.company.project.TestActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
使用此清单 startActivity(new Intent("com.company.project.TestActivity"));
启动我的 Activity,没有任何问题。
但我对这种编码风格并不满意。早些时候,我多次被告知不要使用内联定义的字符串常量,因为它会导致代码的可维护性较差。这确实是一个要点。
因此,首先我在我的 App.java 类(我使用的类)中声明了一个公共静态最终字符串 MY_ACTION = "com.company.project.TestActivity"; ,并通过这个命名常量访问了我的组件方式:
startActivity(new Intent(App.MY_ACTION));
这看起来好多了,但我仍然必须维护同一字符串的两个实例。在 http://developer.android.com/reference/android/R。 styleable.html#AndroidManifestAction_name 阅读
...这也可能是对资源的引用(格式为“@[package:]type:name”)或主题属性(格式为“?[package:][type:]name”)包含此类型的值。
所以我猜想这个字符串定义一次就可以了。我将
放入我的 strings.xml 中并修改了我的应用程序:
public class App ...
public static String MY_ACTION;
@Override
public void onCreate() {
super.onCreate();
MY_ACTION = getString(R.string.MY_ACTION);
...
一切仍然正常
......直到我更改了我的 AndroidManifest.xml 以使用
而不是
也许问题是 R.string 依赖于配置...?但是嘿,我不能如此疯狂地选择这样一个字符串引用,其值可能会从配置更改为配置!developer.android.com 告诉我可以在操作名称中使用引用!我在 API 级别 6、7、end 8 遇到了这种行为。这只是 Android 系统中的一个简单错误吗?或者我
完全困惑了,请帮助我。
Originally my AndroidManifest.xml contained an activity which I reached through its custom action name.
<activity
android:label="HERE I AM"
android:name="TestController">
<intent-filter>
<action android:name="com.company.project.TestActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
With this manifest startActivity(new Intent("com.company.project.TestActivity"));
started my Activity without any problems.
But I was not satisfied with this coding style. Earlyer I was severeal times told not to use in-line defined string constants because it wolud lead to less maintainable code. And it really is a point.
So first I declared a public static final String MY_ACTION = "com.company.project.TestActivity";
in my App.java class (the one I used for ) and reached my component through this named constant this way:
startActivity(new Intent(App.MY_ACTION));
This seemed much better but I still had to maintain two instances of the same string. At http://developer.android.com/reference/android/R.styleable.html#AndroidManifestAction_name a read
... This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
So I guessed It would be fine to have this string defined JUST ONCE. I put <string name="MY_ACTION">com.company.project.TestActivity</string>
in my strings.xml and modified my App:
public class App ...
public static String MY_ACTION;
@Override
public void onCreate() {
super.onCreate();
MY_ACTION = getString(R.string.MY_ACTION);
...
And everything was still O.K.
... until I changed my AndroidManifest.xml to use <action android:name="@string/MY_ACTION" />
instead of <action android:name="com.company.project.TestActivity" />
.
After this change to my biggest surprise the application broke down at runtime complaining about not finding the Activity for my intent. The string reference was probably not parsed correctly. :(
Maybe the problem is that R.string is config dependant...? But hey I cannot be so insane to choose such a string reference whose value may change from config to config! And developer.android.com told me it's O.K. to use references at action names! I experienced this behavior at API levels 6, 7, end 8. Is it just a simple bug in the android system? Or do I misunderstand something?
I am totally puzzled, please help me. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我今天遇到了同样的问题,并去其他地方寻找答案。根据黛安娜·哈克伯恩的说法,我们试图做的事情似乎是不被允许的。显然,您必须在意图过滤器中使用文字字符串。
http://android.bigresource.com/Track/android-zKGKHraw9/
I had the same problem today, and went searching for an answer elsewhere. It appears according to Dianne Hackborne that what we are trying to do is not allowed. You apparently must use the literal strings in intent filters.
http://android.bigresource.com/Track/android-zKGKHraw9/