Open application settings via Intent from preferences.xml

回眸只為那壹抹淺笑 提交于 2019-12-06 03:35:26
ncrypticus

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!

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>
        <Preference android:title="@string/pref_app_version"
        android:summary="@string/pref_app_version_summary">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="YOUR APPLICATION ID"
            android:targetClass="com.my.java.packagename.MyActivity"/>
    </Preference>

Just something I noticed today. If you're working with different product flavours, make sure targetpackage is your applicationId as defined in build.gradle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!