Android:从preferences.xml启动Activity

发布于 2024-12-02 13:13:44 字数 1360 浏览 0 评论 0原文

我想从默认的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

☆獨立☆ 2024-12-09 13:13:44

我也有同样的问题。我通过仅在 AndroidManifest.xml 中声明操作来实现此功能,如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapp" android:versionName="1.3" android:versionCode="4">

...

    <activity android:name=".activities.MyActivity" 
              android:label="@string/my_activity_title"
              android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
           <action android:name="com.example.myapp.activities.MyActivity" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>

然后在我的 Preferences xml 文件中:

<PreferenceCategory
        android:title="@string/my_activity_title">
    <PreferenceScreen
        android:title="@string/my_activity_title" 
        android:summary="@string/my_activity_title">
        <intent android:action="com.example.myapp.activities.MyActivity"/>
    </PreferenceScreen>
</PreferenceCategory>

I was having the same issue. I got this working by only declaring the action in my AndroidManifest.xml, as such:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myapp" android:versionName="1.3" android:versionCode="4">

...

    <activity android:name=".activities.MyActivity" 
              android:label="@string/my_activity_title"
              android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
           <action android:name="com.example.myapp.activities.MyActivity" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>

Then in my Preferences xml file:

<PreferenceCategory
        android:title="@string/my_activity_title">
    <PreferenceScreen
        android:title="@string/my_activity_title" 
        android:summary="@string/my_activity_title">
        <intent android:action="com.example.myapp.activities.MyActivity"/>
    </PreferenceScreen>
</PreferenceCategory>
我很坚强 2024-12-09 13:13:44

我相信 标签必须位于

内,而不是 内。

<PreferenceCategory 
    android:title="@string/titleEtcSetup">
    <Preference
        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" />         
    </Preference>
.....
</PreferenceCategory>

I believe <intent> tag must be inside <Preference>, not <PreferenceScreen>.

<PreferenceCategory 
    android:title="@string/titleEtcSetup">
    <Preference
        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" />         
    </Preference>
.....
</PreferenceCategory>
塔塔猫 2024-12-09 13:13:44

警告! 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 your AndroidManifest.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).

独木成林 2024-12-09 13:13:44

无需添加IntentFilter。您可以通过完全限定名称引用活动:

<intent android:targetPackage="my.notifier.ui"
    android:targetClass="my.notifier.ui.EditCoursesNamesActivity"/>

No need to add IntentFilter. You can refer to activity by fully qualified name:

<intent android:targetPackage="my.notifier.ui"
    android:targetClass="my.notifier.ui.EditCoursesNamesActivity"/>
心清如水 2024-12-09 13:13:44

当我遇到这个问题时,是因为我为我的活动制作了一个子包。当我将它移动到根包中时,首选项屏幕可以启动它。

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.

〃安静 2024-12-09 13:13:44

该解决方案向您展示如何将活动连接到您的首选项标头中。

首先,您的目标活动必须在清单中声明,如下所示:

<activity android:name=".ui.activities.MyActivity">
    <intent-filter>
        <action android:name=".ui.activities.MyActivity" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

请注意,活动和操作的 android:name 是相同的。

现在,在您的preferences.xml 文件中,您只需要声明一个像这样的标头:

<header
    android:title="@string/my_activity_title"
    android:summary="@string/my_activity_summary">
    <intent android:action=".ui.activities.MyActivity"/>
</header>

这就是它的全部内容。

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:

<activity android:name=".ui.activities.MyActivity">
    <intent-filter>
        <action android:name=".ui.activities.MyActivity" />
        <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

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:

<header
    android:title="@string/my_activity_title"
    android:summary="@string/my_activity_summary">
    <intent android:action=".ui.activities.MyActivity"/>
</header>

And that's all there is to it.

我的鱼塘能养鲲 2024-12-09 13:13:44

我也有同样的问题。
并通过这个

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.coding1.myapp">
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            > 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
// ==================== HERE ================================
        <activity
            android:name="yaran.AccountWorker.AuthenticatorActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="AuthenticatorActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
// ========================================================
        <service android:name="yaran.AccountWorker.AuthenticatorService" android:exported="false"  android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                       android:resource="@xml/authenticator" />
        </service>
    </application>

和 Preference 解决:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="General Settings" />

    <Preference
        android:key="account_settings"
        android:title="Account Settings"
        android:summary="">
        <intent
            android:action="AuthenticatorActivity"
            android:targetPackage="com.example.coding1.myapp"
            android:targetClass="yaran.AccountWorker.AuthenticatorActivity" />
    </Preference>
</PreferenceScreen>

I was having the same issue.
and solve by this

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.coding1.myapp">
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            > 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
// ==================== HERE ================================
        <activity
            android:name="yaran.AccountWorker.AuthenticatorActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="AuthenticatorActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
// ========================================================
        <service android:name="yaran.AccountWorker.AuthenticatorService" android:exported="false"  android:process=":auth">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data android:name="android.accounts.AccountAuthenticator"
                       android:resource="@xml/authenticator" />
        </service>
    </application>

and in Preference:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="General Settings" />

    <Preference
        android:key="account_settings"
        android:title="Account Settings"
        android:summary="">
        <intent
            android:action="AuthenticatorActivity"
            android:targetPackage="com.example.coding1.myapp"
            android:targetClass="yaran.AccountWorker.AuthenticatorActivity" />
    </Preference>
</PreferenceScreen>
你的笑 2024-12-09 13:13:44
<Preference>
    <intent
        android:targetPackage="applicationIdFromBuildGradle"
        android:targetClass="targetClassFromJavaFile.YourClassName"/>
</Preference>
<Preference>
    <intent
        android:targetPackage="applicationIdFromBuildGradle"
        android:targetClass="targetClassFromJavaFile.YourClassName"/>
</Preference>
孤独岁月 2024-12-09 13:13:44

我可以通过将意图过滤器中的类别从

android.intent.category.DEFAULT

更改为

android.intent.category.PREFERENCE

http://developer.android.com/reference/android/content/Intent.html#CATEGORY_PREFERENCE

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.PREFERENCE" />
    </intent-filter>
</activity>

我想如果您希望您的操作更加具体,只需删除整个类别节点即可

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
</activity>

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

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.PREFERENCE" />
    </intent-filter>
</activity>

I guess if you want your action to be even more specific just remove the whole category node

<activity
  android:name=".ui.DatapackSelectionActivity"
  android:label="@string/app_name"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.NoTitleBar" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
    </intent-filter>
</activity>
爱人如己 2024-12-09 13:13:44

由于重构包名称,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.

筑梦 2024-12-09 13:13:44

我通过在清单中声明 解决了同样的问题,如下所示:

<activity
    android:name="PeriodosActivity"
    android:label="Periodos"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="Periodos" /> /*Same as in the preference's <intent> element's action attribute*/
        <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

I solved the same issue by declaring the <intent-filter> in the manifest as follows:

<activity
    android:name="PeriodosActivity"
    android:label="Periodos"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="Periodos" /> /*Same as in the preference's <intent> element's action attribute*/
        <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>
渔村楼浪 2024-12-09 13:13:44
<Preference>
<intent
    android:targetPackage="applicationIdFromBuildGradle"
    android:targetClass="targetClassFromJavaFile.YourClassName"/>

请注意在 build.gradle(app) 中使用 packgename
applicationId“com.some.some”
我的包在清单和类中是这样的“com.some.love”
我在运行时遇到异常。这解决了我
参考@style-7

<Preference>
<intent
    android:targetPackage="applicationIdFromBuildGradle"
    android:targetClass="targetClassFromJavaFile.YourClassName"/>

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

£烟消云散 2024-12-09 13:13:44

如果您创建一个,则可以在首选项/设置活动中以编程方式执行此操作:

    final PreferenceScreen renameCourses = (PreferenceScreen) findPreference("renameCourses");
    renameCourses.setIntent(new Intent(getContext(), EditCoursesNamesActivity.class));

You can do this programmatically in a Preferences/Settings Activity if you create one:

    final PreferenceScreen renameCourses = (PreferenceScreen) findPreference("renameCourses");
    renameCourses.setIntent(new Intent(getContext(), EditCoursesNamesActivity.class));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文