Open application settings via Intent from preferences.xml

一个人想着一个人 提交于 2019-12-22 11:15:15

问题


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


回答1:


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!




回答2:


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>



回答3:


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



来源:https://stackoverflow.com/questions/28154832/open-application-settings-via-intent-from-preferences-xml

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