android 12 添加菜单

简介: android 12 添加菜单

1.创建一级菜单

packages\apps\Settings\res\xml\top_level_settings.xml

<com.android.settings.widget.HomepagePreference
        android:fragment="com.android.settings.DeviceStatusSettings"
        android:icon="@drawable/ic_settings_display_white"
        android:key="top_level_device_status"
        android:order="-80"
        android:title="@string/device_status_settings"
        android:summary="@string/device_status_dashboard_summary"
        settings:controller="com.android.settings.devicestatus.TopLevelDeviceStatusPreferenceController"/>

DeviceStatusSettings

@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class DeviceStatusSettings extends DashboardFragment {
    private static final String TAG = "DeviceStatusSettings";
    private static final String KET_SPK_MODE_SETTINGS = "spk_mode_settings";
 
    @Override
    public int getMetricsCategory() {
        return SettingsEnums.DISPLAY;
    }
 
    @Override
    protected String getLogTag() {
        return TAG;
    }
 
    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.device_status_setting;
    }
 
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
    }
 
    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getSettingsLifecycle());
    }
 
    @Override
    public int getHelpResource() {
        return R.string.help_uri_display;
    }
 
    private static List<AbstractPreferenceController> buildPreferenceControllers(
            Context context, Lifecycle lifecycle) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new SpkModePreferenceController(context, KET_SPK_MODE_SETTINGS));
        return controllers;
    }
 
    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.device_status_setting) {
 
                @Override
                public List<AbstractPreferenceController> createPreferenceControllers(
                        Context context) {
                    return buildPreferenceControllers(context, null);
                }
            };
}

TopLevelDeviceStatusPreferenceController

public class TopLevelDeviceStatusPreferenceController extends BasePreferenceController {
 
    public TopLevelDeviceStatusPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }
 
    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
}

2.创建二级菜单

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="display_settings_screen"
    android:title="@string/device_status_settings"
    settings:keywords="@string/keywords_display">
    <PreferenceCategory
        android:title="@string/device_spk_mode">
        <com.android.settingslib.RestrictedPreference
            android:key="device_spk_mode"
            android:title="@string/device_spk_mode"
            android:summary="@string/summary_placeholder"
            android:fragment="com.android.settings.devicestatus.SpkModeSettings"
            settings:controller="com.android.settings.devicestatus.SpkModePreferenceController"/>
    </PreferenceCategory>
</PreferenceScreen>

SpkModeSettings

public class SpkModeSettings extends RadioButtonPickerFragment {
 
    private static final String TAG = "SpkModeSettings";
    public static final int FALLBACK_VALUE = 0;
    private static final String SPK_MODE_BROADCAST = "android.intent.action.SPK_MODE";
    private static final String SPK_MODE_PERSIST = "persist.realbom.spkmode";
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
 
    @Override
    protected List<? extends CandidateInfo> getCandidates() {
        final String[] entries = entries();
        final String[] values = keys();
        final List<SpkModeControlCandidateInfo> candidates = new ArrayList<>();
 
        if (entries == null || entries.length <= 0) return null;
        if (values == null || values.length != entries.length) {
            throw new IllegalArgumentException("Entries and values must be of the same length.");
        }
 
        for (int i = 0; i < entries.length; i++) {
            candidates.add(new SpkModeControlCandidateInfo(entries[i], values[i]));
        }
 
        return candidates;
    }
 
    private String[] entries() {
        return getResources().getStringArray(R.array.spk_mode_entries);
    }
 
    private String[] keys() {
        return getResources().getStringArray(R.array.spk_mode_values);
    }
 
    @Override
    protected String getDefaultKey() {
        return getCurrentSkpModeValue(getContext());
    }
 
    @Override
    protected boolean setDefaultKey(String key) {
        setCurrentSkpModeValue(getContext(), key);
        return true;
    }
 
    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.spk_mode;
    }
 
    @Override
    public int getMetricsCategory() {
        return 1;
    }
    
    private String getCurrentSkpModeValue(Context context) {
        if (context == null) {
            return Long.toString(FALLBACK_VALUE);
        } else {
            return SystemProperties.get(SPK_MODE_PERSIST, "0");
        }
    }
 
    private void setCurrentSkpModeValue(Context context, String key) {
        try {
            if (context != null) {
                Log.d(TAG,"set current value ==  "+key);
                int value = Integer.parseInt((String) key);
                SystemProperties.set(SPK_MODE_PERSIST, String.valueOf(value));
                //Settings.System.putInt(context.getContentResolver(), SPK_MODE, value);
                setSkpModeValue(context,value);
            }
        } catch (NumberFormatException e) {
            Log.e(TAG, "could not persist screen timeout setting", e);
        }
    }
 
    @Override
    protected void onSelectionPerformed(boolean success) {
        super.onSelectionPerformed(success);
        //getActivity().finish();
    }
 
    private final class SpkModeControlCandidateInfo extends CandidateInfo {
        private final String name;
        private final String key;
 
        SpkModeControlCandidateInfo(String title, String value) {
            super(true);
 
            name = title;
            key = value;
        }
 
        @Override
        public CharSequence loadLabel() {
            return name;
        }
 
        @Override
        public Drawable loadIcon() {
            return null;
        }
 
        @Override
        public String getKey() {
            return key;
        }
    }
 
    private void setSkpModeValue(Context context, int value){
        Intent intent = new Intent();
        intent.setAction(SPK_MODE_BROADCAST);
        //intent.putExtra("com",4);
        intent.putExtra("value",value);
        context.sendBroadcast(intent);
    }
 
}

SpkModePreferenceController

public class SpkModePreferenceController extends BasePreferenceController {
    public static String PREF_NAME = "spk_mode";
    public static String TAG = "SpkModePreferenceController";
    public static final int FALLBACK_VALUE = 0;
    private final CharSequence[] mEntries;
    private final CharSequence[] mValues;
    private static final String SPK_MODE_BROADCAST = "android.intent.action.SPK_MODE";
    private static final String SPK_MODE_PERSIST = "persist.realbom.spkmode";
 
    public SpkModePreferenceController(Context context, String key) {
        super(context, key);
        mEntries = context.getResources().getStringArray(R.array.spk_mode_entries);
        mValues = context.getResources().getStringArray(R.array.spk_mode_values);
    }
 
    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
 
    @Override
    public void updateState(Preference preference) {
        final long curValue = Long.valueOf(SystemProperties.get(SPK_MODE_PERSIST,"0"));
        updatePreferenceDescription(preference, curValue);
        Log.d(TAG, "updateState currentValue= "+String.valueOf(curValue));
    }
    
    public static CharSequence getDescription(long curValue, CharSequence[] entries, CharSequence[] values) {
        if (curValue < 0 || entries == null || values == null || values.length != entries.length) {
            return null;
        }
 
        for (int i = 0; i < values.length; i++) {
            long temp = Long.parseLong(values[i].toString());
            if (curValue == temp) {
                Log.d(TAG,"Description entries == "+entries[i]);
                return entries[i];
            }
        }
        return null;
    }
    
    private void updatePreferenceDescription(Preference preference,long curValue) {
        final CharSequence summary;
 
        summary = getDescription(curValue, mEntries, mValues);
        preference.setSummary(summary);
    } 
}

 

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/device_spk_mode"/>

 

 


目录
相关文章
|
4月前
|
XML API Android开发
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
本文介绍了如何使用androidx.preference库快速创建具有一级和二级菜单的Android设置界面的步骤和示例代码。
129 1
码农之重学安卓:利用androidx.preference 快速创建一、二级设置菜单(demo)
|
6月前
|
XML Java Android开发
34. 【Android教程】菜单:Menu
34. 【Android教程】菜单:Menu
122 2
|
6月前
|
开发工具 Android开发 开发者
Android Studio中两个让初学者崩溃菜单
Android Studio中两个让初学者崩溃菜单
56 0
|
4月前
|
API Android开发
Android使用AlertDialog实现弹出菜单
本文分享了在Android开发中使用AlertDialog实现弹出菜单的方法,并通过代码示例和错误处理,展示了如何避免因资源ID找不到导致的crash问题。
68 1
|
7月前
|
Java Android开发
Android 长按电源键弹出的GlobalActions菜单
Android 长按电源键弹出的GlobalActions菜单
172 1
|
7月前
|
Java Android开发
Android 长按桌面显示菜单的代码
Android 长按桌面显示菜单的代码
45 0
|
Android开发
Android 中选项菜单(Option menu)的用法
Android 中选项菜单(Option menu)的用法
198 0
|
7月前
|
Android开发
[Android]DrawerLayout滑动菜单+NavigationView
[Android]DrawerLayout滑动菜单+NavigationView
78 0
|
7月前
|
XML Java Android开发
Android App手势冲突处理中上下左右滑动的处理以及侧滑边缘菜单的讲解及实战(附源码 可直接使用)
Android App手势冲突处理中上下左右滑动的处理以及侧滑边缘菜单的讲解及实战(附源码 可直接使用)
500 0
|
7月前
|
XML Java Android开发
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
639 0