您的 PreferenceActivity 实现容易受到片段注入的影响

Your implementation of PreferenceActivity is vulnerable to fragment injection

这是我的 java class.

public class MyPrefrences extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);
}

}

在我的项目中我使用了preferences.xml

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
        android:title="@string/steps_settings_title">

    <ListPreference
            android:key="sensitivity"
            android:title="@string/setting"
            android:summary="@string/setting_details"
            android:entries="@array/preference"
            android:entryValues="@array/preference_values"
            android:dialogTitle="@string/setting_title"
            android:defaultValue="30" />

    <ListPreference
            android:key="operation_level"
            android:title="@string/operation_level_setting"
            android:summary="@string/operation_level_setting_details"
            android:entries="@array/operation_level_preference"
            android:entryValues="@array/operation_level_preference_values"
            android:dialogTitle="@string/operation_level_setting_title"
            android:defaultValue="30" />

    <ListPreference
            android:key="units"
            android:title="@string/units_setting"
            android:summary="@string/units_setting_details"
            android:entries="@array/units_preference"
            android:entryValues="@array/units_preference_values"
            android:dialogTitle="@string/units_setting_title"
            android:defaultValue="imperial" />

    <com.example.myapp.preferences.StepLengthPreference
            android:key="length"
            android:title="@string/length_setting"
            android:summary="@string/length_setting_details"
            android:dialogTitle="@string/length_setting_title"
            android:defaultValue="20" />

    <com.example.myapp.preferences.BodyWeightPreference
            android:key="body_weight"
            android:title="@string/weight_setting"
            android:summary="@string/weight_setting_details"
            android:dialogTitle="@string/weight_setting_title"
            android:defaultValue="50" />

    <ListPreference
            android:key="exercise_type"
            android:title="@string/type_setting"
            android:summary="@string/type_setting_details"
            android:entries="@array/type_preference"
            android:entryValues="@array/type_preference_values"
            android:dialogTitle="@string/type_setting_title"
            android:defaultValue="running" />

    <ListPreference
            android:key="maintain"
            android:title="@string/maintain_setting"
            android:summary="@string/maintain_setting_details"
            android:entries="@array/maintain_preference"
            android:entryValues="@array/maintain_preference_values"
            android:dialogTitle="@string/maintain_setting_title"
            android:defaultValue="none" />

</PreferenceCategory>


</PreferenceScreen>

我如何根据新的 google 安全策略修复此问题。 您的应用程序实施不安全,其中 PreferenceActivity classes 可能容易受到片段注入的影响。 在清单中为受影响的 PreferenceActivity 设置 exported=false isValidFragment 的错误实现 他们怎么可能修好?

这段代码非常适合我:

public class MyPrefrence extends PreferenceActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new MyPreferenceFragment())
            .commit();

}

public static class MyPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

}