Android SystemUI相关定制(三)

本文介绍如何通过修改安卓系统的特定文件实现底部导航栏的动态隐藏与显示功能,包括添加广播接收器、设置开关偏好等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

底部导航栏:

1.隐藏底部导航栏

 ——————————————————————————————————————————

2.导航栏动态隐藏、显示

1、frameworks/base/core/java/android/content/Intent.java文件,在文件末尾插入如下代码:

2、

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java中添加过滤广播

3、在广播mBroadcastReceiver中添加判断项

private void setSystemBarVisibility(int visibility) {

        if (DEBUG) Log.v(TAG, "setSystemBarVisibility: " + visibility);

        if (visibility == View.GONE && mNavigationBarView != null) {

            try {

                mWindowManager.removeViewImmediate(mNavigationBarView);

                Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY , 0);

Log.d("daibin","statusbar setSystemBarVisibility gone"); 

            } catch (IllegalArgumentException e) {

                Log.w(TAG, "IllegalArgumentException: " + e);

            }

            mNavigationBarView = null;

        } else if (visibility == View.VISIBLE && mNavigationBarView == null) {

            try {

                createNavigationBar();

                Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY , 1);

Log.d("daibin","statusbar setSystemBarVisibility visibility"); 

            } catch (WindowManager.BadTokenException e) {

                // ignore

                Log.w(TAG, "BadTokenException: " + e.getMessage());

            } catch (RuntimeException e) {

                // don't crash if something else bad happens, for example a

                // failure loading resources because we are loading from an app

                // on external storage that has been unmounted.

                Log.w(TAG, "RuntimeException: " + e);

            }

        }

    }

4、系统设置添加导航栏隐藏、显示功能

 (1)找到packages/apps/Settings/res/values-zh-rCN/strings.xml文件,插入以下代码:

<!-- System Bar settings title-->

    <string name="system_bar_settings_title">系统栏显示</string>

    <!-- System Bar settings summary-->

    <string name="system_bar_settings_summary">控制系统栏显示或隐藏</string>

    (2)找到apps/Settings/res/values/strings.xml文件,插入以下代码:

<!-- System Bar settings title-->

    <string name="system_bar_settings_title">System Bar Display</string>

    <!-- System Bar settings summary-->

    <string name="system_bar_settings_summary">Control System Bar show or hide</string>

  (3)找到packages/apps/Settings/res/xml/display_settings.xml文件,插入以下代码:

<SwitchPreference

    android:key="navigation_bar"

    android:title="@string/system_bar_settings_title"

    android:summary="@string/system_bar_settings_summary"

    android:persistent="false" />

注意:SwitchPreference放置的位置决定了显示的位置

5、找到frameworks/base/core/java/android/provider/Settings.java,添加以下内容

 

6、新建导航栏隐藏、显示控制类

位置:packages/apps/Settings/src/com/android/settings/display/

public class NavigationBarDisplayController extends AbstractPreferenceController

        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {



    private static final String KEY_NAVIGATION_DISPLAY = "navigation_bar";



    private Preference mPreference;

    private Context mContext;



    public NavigationBarDisplayController(Context context) {

        super(context);

        mContext = context;



        IntentFilter filter = new IntentFilter();

        filter.addAction(Intent.ACTION_SYSTEM_BAR_SHOW);

        filter.addAction(Intent.ACTION_SYSTEM_BAR_HIDE);

        mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null);

    }



    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override

        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if (Intent.ACTION_SYSTEM_BAR_SHOW.equals(action)) {

                if (mPreference != null)

                    ((SwitchPreference) mPreference).setChecked(true);

            } else if (Intent.ACTION_SYSTEM_BAR_HIDE.equals(action)) {

                if (mPreference != null)

                    ((SwitchPreference) mPreference).setChecked(false);

            }

        }

    };



    @Override

    public boolean isAvailable() {

        return true;

    }



    @Override

    public String getPreferenceKey() {

        return KEY_NAVIGATION_DISPLAY;

    }



    @Override

    public boolean onPreferenceChange(Preference preference, Object newValue) {

        if (mPreference == null) mPreference = preference;

        boolean value = (Boolean) newValue;

        mContext.sendBroadcast(new Intent(value ? Intent.ACTION_SYSTEM_BAR_SHOW : Intent.ACTION_SYSTEM_BAR_HIDE));

        Log.d("daibin", "onPreferenceChange putInt");

        //Settings.System.putInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY, value ? 1 : 0);

        return true;

    }



    @Override

    public void updateState(Preference preference) {

        super.updateState(preference);

        if (mPreference == null) mPreference = preference;

        // update navigation bar

        int value = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SYSTEM_BAR_DISPLAY, 1);

        ((SwitchPreference) preference).setChecked(value == 1);

    }

}

注意:getPreferenceKey()要和布局中的SwitchPreference的key对应起来

7、找到

packages/apps/Settings/src/com/android/settings/DisplaySettings.java添加以下内容

 

8、用法:

// 显示

adb shell am broadcast -a android.intent.action.SYSTEM_BAR_SHOW

// 隐藏

adb shell am broadcast -a android.intent.action.SYSTEM_BAR_HIDE

或者

// 显示

Intent intent = new Intent();

intent.setAction("android.intent.action.SYSTEM_BAR_SHOW");

sendBroadcast(intent);

// 隐藏

Intent intent = new Intent();

intent.setAction("android.intent.action.SYSTEM_BAR_HIDE");

sendBroadcast(intent);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值