IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Q - Settings 页面添加一级菜单 -> 正文阅读

[移动开发]Android Q - Settings 页面添加一级菜单

Android Q - Settings页面添加二级选项_感觉不怎么会的博客-CSDN博客

本次修改基本同上一篇文章,在一级菜单中的最后添加 OTG 选项,简单记录下。

文件清单,依旧位于 packages/apps/Settings :

  • res/xml/top_level_settings.xml
  • res/xml/otg_settings.xml
  • src/com/android/settings/otg/OTGPreferenceController.java
  • src/com/android/settings/otg/OTGSettingFragment.java
  • src/com/android/settings/otg/TopLevelOTGPreferenceController.java

下面简单说明。

1、修改布局文件

路径:res/xml/top_level_settings.xml

说明:这个文件就是打开 Settings 应用的首页

...
    <Preference
        android:key="top_level_otg"
        android:summary="otg"
        android:title="@string/settings_otg_tittle"
        android:order="150"
        android:icon="@drawable/ic_homepage_otg"
        android:fragment="com.android.settings.otg.OTGSettingFragment"
        settings:controller="com.android.settings.otg.TopLevelOTGPreferenceController"/>

</PreferenceScreen>

路径:res/xml/otg_settings.xml

说明:进入 OTG 菜单后的布局文件,领导只说需要一个开关选项,功能实现可仿照上一篇文章实现。


<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="otg_setting"
    android:title="@string/settings_otg_tittle"
    settings:initialExpandedChildrenCount="1">
    <!--lichang add OTG-->
    <com.android.settingslib.RestrictedSwitchPreference
        android:key="usb_otg"
        android:title="@string/settings_otg_text"
        android:summary="@string/settings_otg_summary"
        android:order="-25"
        settings:controller="com.android.settings.otg.OTGPreferenceController"
        />
</PreferenceScreen>

?2、功能实现

路径:src/com/android/settings/otg/TopLevelOTGPreferenceController.java
说明:菜单选项


package com.android.settings.otg;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class TopLevelOTGPreferenceController extends BasePreferenceController {
    public TopLevelOTGPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }
    @Override
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
    @Override
    public CharSequence getSummary() {
        return mContext.getString(R.string.settings_otg_text);
    }
}

路径:src/com/android/settings/otg/OTGSettingFragment.java

说明:这个就是点击一级菜单后进入的页面,加载二级页面。(有的参数不太清楚怎么添加,清楚之后再更新)


/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.otg;
import static com.android.settings.network.MobilePlanPreferenceController.MANAGE_MOBILE_PLAN_DIALOG_ID;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.provider.SearchIndexableResource;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.development.featureflags.FeatureFlagPersistent;
import com.android.settings.network.MobilePlanPreferenceController.MobilePlanPreferenceHost;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.wifi.WifiMasterSwitchPreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable;
import com.android.settings.deviceinfo.BuildNumberPreferenceController;
import com.android.settings.otg.OTGPreferenceController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SearchIndexable
public class OTGSettingFragment extends DashboardFragment {
    private static final String TAG = "OTGDashboardFrag";
    public static final String ARG_SHOW_EXPAND_BUTTON = "showExpandButton";
    public static final String EXTRA_SHOW_AWARE_DISABLED = "show_aware_dialog_disabled";
    private static final int COUNT_INVALID = -1;
    private Fragment mFragment;
    /*UNISOC: bug for 966475 @{*/
    private AlertDialog mMobilePlanDialog;
    /*UNISOC: @}*/
    private int mChildrenCount = COUNT_INVALID;
    @Override
    public int getMetricsCategory() {
        //应该是这种格式的 SettingsEnums.SETTINGS_NETWORK_CATEGORY;
        //但是我不知道如何添加,暂时默认这个数值吧~
        return 9232;
    }
    @Override
    protected String getLogTag() {
        return TAG;
    }
    @Override
    protected int getPreferenceScreenResId() {
        return R.xml.otg_settings;
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        /*UNISOC: Modify for bug1145683, We do not want to display an advanced button if
         * linking to this screen from condition @{*/
        Bundle args = getArguments();
        if (args != null) {
            if (!args.getBoolean(ARG_SHOW_EXPAND_BUTTON, true)) {
                mChildrenCount = Integer.MAX_VALUE;
            }
        }
        use(OTGPreferenceController.class).setFragment(this);
    }
    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        return buildPreferenceControllers(context, getSettingsLifecycle(),
                this /* fragment */);
    }
    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
                                                                                 Lifecycle lifecycle, Fragment fragment) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        return controllers;
    }
}

路径:src/com/android/settings/otg/OTGPreferenceController.java

说明:这个基本同上一篇文章中的以太网开关控制一样,甚至比之前写的更少。


/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.otg;
import android.os.Bundle;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.utils.ThreadPool;
import com.android.settings.utils.ReflectUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class OTGPreferenceController extends TogglePreferenceController {
    private static final String TAG = "OTGPreferenceController";
    public OTGPreferenceController(Context context, String key) {
        super(context, key);
    }
    private Fragment mFragment;
    private SwitchPreference mOTGPreference;
    private SharedPreferences mSharedPreference;
    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        android.util.Log.d(TAG, "handlePreferenceTreeClick: 測試" + preference.getKey());
        changeState(!isChecked());
        return false;
    }
    @Override
    @AvailabilityStatus
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }
    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        android.util.Log.d(TAG, "displayPreference: " );
        if (isAvailable()) {
            mOTGPreference = screen.findPreference(getPreferenceKey());
            if (isChecked())
                mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_open));
            else
                mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_close));
            mOTGPreference.setChecked(isChecked());
        }
    }
    public void init(DashboardFragment fragment) {
        final Context context = fragment.getContext();
    }
    public void setFragment(Fragment hostFragment) {
        mFragment = hostFragment;
    }
    @Override
    public boolean setChecked(boolean isChecked) {
        android.util.Log.d(TAG, "setChecked: " + isChecked);
        return false;
    }
    public void changeState(boolean state) {
        android.util.Log.d(TAG, "changeState to be = " + state);
        if (state) {
            mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_open));
            mOTGPreference.setChecked(true);
            ReflectUtil.setProperty("persist.flyscale.otg", "1");
        } else {
            mOTGPreference.setSummary(mContext.getString(R.string.settings_eth_close));
            mOTGPreference.setChecked(false);
            ReflectUtil.setProperty("persist.flyscale.otg", "0");
        }
        android.util.Log.d(TAG, "changeState: currentState is = " + isChecked());
    }
    @Override
    public boolean isChecked() {
        return TextUtils.equals(ReflectUtil.getProperty("persist.flyscale.otg", "0"), "1");
    }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-05-09 12:51:06  更:2022-05-09 12:51:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 23:48:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码