Android:从preferences.xml启动Activity
我想从默认的preferences.xml启动一个Activity,使用<意图>标签。活动已经过充分测试,问题不在于此。 (我在我的应用程序中扩展了 PreferenceActivity,因此preferences.xml 是“随之而来”的) 请看一下代码,有什么问题吗?
preferences.xml:
....
<PreferenceCategory
android:title="@string/titleEtcSetup">
<PreferenceScreen
android:key="renameCourses"
android:title="@string/titleRenameCourses"
android:summary="@string/textRenameDisplayedCoursesNames">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="my.notifier.ui"
android:targetClass="my.notifier.ui.EditCoursesNamesActivity" />
</PreferenceScreen>
.....
</PreferenceCategory>
.....
manifest.xml:
....
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.notifier.ui"....
....
<activity android:name=".EditCoursesNamesActivity" android:label="@string/titleRenameCourses">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
.....
当我按下“renameCourses item”时,活动没有调用,什么也没有发生。 LogCat 是“清晰的”,没有错误或警告。我搜索了很多,但没有找到解决方案,也许我只是错过了一些东西......请帮助!
I would like to start an Activity from a default preferences.xml, with < intent > tag. The Activities are well tested, the problem is not with that. (I'm extending PreferenceActivity in my app, so the preferences.xml is "comes" with that)
Please look at the code, what's wrong?
preferences.xml:
....
<PreferenceCategory
android:title="@string/titleEtcSetup">
<PreferenceScreen
android:key="renameCourses"
android:title="@string/titleRenameCourses"
android:summary="@string/textRenameDisplayedCoursesNames">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="my.notifier.ui"
android:targetClass="my.notifier.ui.EditCoursesNamesActivity" />
</PreferenceScreen>
.....
</PreferenceCategory>
.....
manifest.xml:
....
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.notifier.ui"....
....
<activity android:name=".EditCoursesNamesActivity" android:label="@string/titleRenameCourses">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
.....
The Activity isn't calling when I press the "renameCourses item", nothing happens. The LogCat is "clear", no errors or warnings. I was searching a lot, and I didn't find a solution, maybe I just missed something... Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我也有同样的问题。我通过仅在 AndroidManifest.xml 中声明操作来实现此功能,如下所示:
然后在我的 Preferences xml 文件中:
I was having the same issue. I got this working by only declaring the action in my AndroidManifest.xml, as such:
Then in my Preferences xml file:
我相信
标签必须位于
内,而不是
内。I believe
<intent>
tag must be inside<Preference>
, not<PreferenceScreen>
.警告!
targetPackage
的值应该是应用程序的包 ID,如AndroidManifest.xml
文件(您在 Gradle 构建文件中定义)的根元素中声明的那样。它不必与您的 Activity 类的 Java 包相同(人们通常将它们放在"ui"
的子包中)。因此,在您的具体情况下,我敢打赌您的
targetPackage
应该是"my.notifier"
,而不是"my.notifier.ui"
(我必须查看清单才能确定)。Caution! The value of
targetPackage
should be the package id of the app, as declared in the root element of yourAndroidManifest.xml
file (which you define in your Gradle build file). It is not necessary the same as the Java package of your Activity class (people usually put them in a subpackage of"ui"
).So in your specific case, I bet you the
targetPackage
should be"my.notifier"
, not"my.notifier.ui"
(I would have to see the manifest to be sure).无需添加IntentFilter。您可以通过完全限定名称引用活动:
No need to add IntentFilter. You can refer to activity by fully qualified name:
当我遇到这个问题时,是因为我为我的活动制作了一个子包。当我将它移动到根包中时,首选项屏幕可以启动它。
When I had this problem it was because I had made a sub-package for my activities. When I moved it into the root package the Preference screen could launch it.
该解决方案向您展示如何将活动连接到您的首选项标头中。
首先,您的目标活动必须在清单中声明,如下所示:
请注意,活动和操作的 android:name 是相同的。
现在,在您的preferences.xml 文件中,您只需要声明一个像这样的标头:
这就是它的全部内容。
This solution shows you how to wire in an activity into your preferences headers.
First, your target activity must be declared in the manifest like this:
Notice here that the android:name for the activity and action are the same.
Now in your preferences.xml file you need only to declare a header like this:
And that's all there is to it.
我也有同样的问题。
并通过这个
androidManifest.xml
和 Preference 解决:
I was having the same issue.
and solve by this
androidManifest.xml
and in Preference:
我可以通过将意图过滤器中的类别从
android.intent.category.DEFAULT
更改为
android.intent.category.PREFERENCE
http://developer.android.com/reference/android/content/Intent.html#CATEGORY_PREFERENCE
我想如果您希望您的操作更加具体,只需删除整个类别节点即可
I was able to fix this by changing the category in the intent filter from
android.intent.category.DEFAULT
to
android.intent.category.PREFERENCE
http://developer.android.com/reference/android/content/Intent.html#CATEGORY_PREFERENCE
I guess if you want your action to be even more specific just remove the whole category node
由于重构包名称,targetPackage 和 targetClass(prefix) 可能会有所不同。检查它的简单方法是,您可以删除清单中的活动并调用
startActivity()
然后您将在 logCat 中看到此错误:“无法找到显式活动类 {'targetPackage'/'targetClass'}”。这些是您应该写入“首选项”>“意图”中的正确名称。不需要意图过滤器。targetPackage and targetClass(prefix) may differ because of refactoring package name. Easy way to check it you can delete activity in manifest and call
startActivity()
then you will see this error in logCat: "Unable to find explicit activity class {'targetPackage'/'targetClass'}". Those are the right names you should write into Preference>intent. Intent-filter is not needed.我通过在清单中声明
解决了同样的问题,如下所示:I solved the same issue by declaring the
<intent-filter>
in the manifest as follows:请注意在 build.gradle(app) 中使用 packgename
applicationId“com.some.some”
我的包在清单和类中是这样的“com.some.love”
我在运行时遇到异常。这解决了我
参考@style-7
Please care use packgename in build.gradle(app)
applicationId "com.some.some"
my package was like this "com.some.love" in manifest and class
I got exception while running .This Solved me
reference @style-7
如果您创建一个,则可以在首选项/设置活动中以编程方式执行此操作:
You can do this programmatically in a Preferences/Settings Activity if you create one: