Android: Start Activity from preferences.xml

后端 未结 12 1974
轮回少年
轮回少年 2020-12-04 13:16

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 Prefe

相关标签:
12条回答
  • 2020-12-04 13:20

    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>
    
    0 讨论(0)
  • 2020-12-04 13:21
    <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

    0 讨论(0)
  • 2020-12-04 13:28

    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>
    
    0 讨论(0)
  • 2020-12-04 13:32

    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.

    0 讨论(0)
  • 2020-12-04 13:33

    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).

    0 讨论(0)
  • 2020-12-04 13:35

    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>
    
    0 讨论(0)
提交回复
热议问题