Search code examples
androidxmlandroid-intentpreferences

Open application settings via Intent from preferences.xml


I would like to open the application settings by clicking on a preferences entry. So I added an intent to the preferences.xml

    <PreferenceScreen
        android:key="DELETE_DATA"
        android:title="@string/pref_delete_data">
        <intent android:action="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
    </PreferenceScreen>

and I've added an Intent-filter to the AndroidManifest.xml

    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".MainActivity">
        <intent-filter>
            <action android:name="android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
...

With the code above, there is no action or error. But I don't know why... If I'm removing the <category> there comes an error, so the intent is fired. Any ideas?

Device: HTC One M8 with Android 4.4.4


Solution

  • For others interested in how OP used the OnPreferenceClickListener, check out this answer from a similar question. But to launch an activity directly from an intent, this worked for me:

    <?xml version="1.0" encoding="utf-8"?>
    
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
      <PreferenceCategory
        android:title="@string/pref_title_category_name">
      <PreferenceScreen
        android:title="@string/pref_title_activity_name"
        android:key="pref_launch_settings">
      <intent
        android:action="android.intent.action.VIEW"
        android:data="<data>"
        android:targetPackage="<package name>"
        android:targetClass="<target class>" />
      </PreferenceScreen>
    </PreferenceCategory>
    

    data = Full package name plus activity name. I.e., com.example.myapp.ActivityName

    package name = Package name as defined in the root of your manifest file. I.e., com.example.myapp

    targetClass = full package path again, i.e., com.example.myapp.ActivityName

    Note that I'm using the <intent> tag this way inside of a PreferenceScreen, rather than a PreferenceCategory.

    Hope that helps!