Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 914
ray Avatar asked Sep 10 '26 06:09

ray


2 Answers

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!

like image 149
ncrypticus Avatar answered Sep 12 '26 19:09

ncrypticus


Set android:data to your package name. Replace com.myapp (below) with the name of your app's package name found in the manifest file.

  <PreferenceScreen
        android:title="@string/system_settings_preference_title" >
        <intent android:action="android.settings.APPLICATION_DETAILS_SETTINGS"
            android:data="package:com.myapp"/>
    </PreferenceScreen>
like image 37
eric.mcgregor Avatar answered Sep 12 '26 18:09

eric.mcgregor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!