概述
总目录:RK3566/RK3568 Android 11 定制大全
在系统服务中增加显示/隐藏状态栏方法,在上层app动态调用显示/隐藏状态栏方法,设备关机和重启后也能继续生效。
创建全局变量
1.定义全局变量
在frameworks/base/core/java/android/provider/Settings.java中添加
/**
* hide status bar
* @hide
*/
public static final String SYSTEM_HIDE_STATUS_BAR = "hide_status_bar";
2.定义全局变量默认值
在frameworks/base/packages/SettingsProvider/res/values/defaults.xml添加
<bool name="def_hide_status_bar">false</bool>
3.初始化默认值
在frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java添加
loadBooleanSetting(stmt, Settings.System.SYSTEM_HIDE_STATUS_BAR,
R.bool.def_hide_status_bar);
在系统服务中添加显示和隐藏方法
1.在AIDL中添加方法
frameworks/base/core/java/com/custom/ICustomService.aidl
/* 显示状态栏*/
void showStatusBar();
/* 隐藏状态栏*/
void hideStatusBar();
2.在Manager中添加方法
frameworks/base/core/java/com/custom/CustomManager.java
/**
* 显示状态栏
* @hide
*/
public void showStatusBar(){
try {
if (mService!=null){
mService.showStatusBar();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
/**
* 隐藏状态栏
* @hide
*/
public void hideStatusBar(){
try {
if (mService!=null){
mService.hideStatusBar();
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
3.在Service中添加显示隐藏状态栏实现
frameworks/base/services/core/java/com/custom/CustomService.java
/**
* 显示状态栏。
*/
public void showStatusBar() {
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.SYSTEM_HIDE_STATUS_BAR,0);
Intent intent = new Intent("action.ACTION_HIDE_STATUS_BAR");
mContext.sendBroadcast(intent);
}
/**
* 隐藏状态栏。
*/
public void hideStatusBar() {
Settings.System.putInt(
mContext.getContentResolver(), Settings.System.SYSTEM_HIDE_STATUS_BAR,1);
Intent intent = new Intent("action.ACTION_HIDE_STATUS_BAR");
mContext.sendBroadcast(intent);
}
在SystemUI中监听广播控制状态栏的显示和隐藏
1.在StatusBar.java中添加
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
index f6d0a13503..fdceb69307 100644
--- a/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
+++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java
@@ -264,6 +264,8 @@ public class StatusBar extends SystemUI implements DemoMode,
private boolean mHideNavStatus = true;
private static final int MSG_HIDE_NAVIGATION_STATUS = 10001;
+ public static final String ACTION_HIDE_STATUS_BAR = "action.ACTION_HIDE_STATUS_BAR"; //隐藏状态栏
+
private static final String BANNER_ACTION_CANCEL =
"com.android.systemui.statusbar.banner_action_cancel";
@@ -1117,6 +1119,7 @@ public class StatusBar extends SystemUI implements DemoMode,
}
setNavigationStatus();
+ setStatusBar();
if (ENABLE_LOCKSCREEN_WALLPAPER && mWallpaperSupported) {
mLockscreenWallpaper = mLockscreenWallpaperLazy.get();
@@ -1312,6 +1315,31 @@ public class StatusBar extends SystemUI implements DemoMode,
}
}
+ private void setStatusBar() {
+ boolean hideStatusBar = Settings.System.getInt(mContext.getContentResolver(), Settings.System.SYSTEM_HIDE_STATUS_BAR, 0) != 0;
+
+ if (hideStatusBar)
+ hideStatusBar();
+ else {
+ showStatusBar();
+ }
+ }
+
+ private void showStatusBar() {
+ if (mPhoneStatusBarWindow != null) {
+ Log.d(TAG, "====== show StatusBar ======");
+ mPhoneStatusBarWindow.setVisibility(View.VISIBLE);
+ }
+ }
+
+ private void hideStatusBar() {
+ if (mPhoneStatusBarWindow != null) {
+ Log.d(TAG, "====== hide StatusBar ======");
+ mPhoneStatusBarWindow.setVisibility(View.GONE);
+ }
+ }
+
+
@NonNull
@Override
@@ -1339,6 +1367,7 @@ public class StatusBar extends SystemUI implements DemoMode,
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG);
filter.addAction(ACTION_HIDE_NAVIGATION);
+ filter.addAction(ACTION_HIDE_STATUS_BAR);
mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, null, UserHandle.ALL);
}
@@ -2862,6 +2891,8 @@ public class StatusBar extends SystemUI implements DemoMode,
else if(ACTION_HIDE_NAVIGATION.equals(action)) { //接收广播
mHandler.removeMessages(MSG_HIDE_NAVIGATION_STATUS);
mHandler.sendEmptyMessageDelayed(MSG_HIDE_NAVIGATION_STATUS, 300);
+ }else if (ACTION_HIDE_STATUS_BAR.equals(action)){
+ setStatusBar();
}
}
};
编译
./build.sh -Au
编译完成后烧录update.img
在app中调用
1.复制out\soong\.intermediates\frameworks\base\framework-minus-apex\android_common\jarjar\framework-minus-apex.jar 到AndroidStudio 项目的libs中
在build.gradle --- dependencies中添加依赖
compileOnly files('libs\\framework.jar')
2.在代码中调用
CustomManager customManager = (CustomManager) getSystemService("custom_service");
//显示状态栏
customManager.showStatusBar();
//隐藏状态栏
customManager.hideStatusBar();