iT邦幫忙

0

android禁止使用STATUS_BAR

我在APP上嘗試阻止使用者使用STATUS_BAR,可是android.permission.STATUS_BAR為系統級app才能使用的權限,導致disableStatusBar()執行時會拋出java.lang.SecurityException: StatusBarManagerService: Neither user 10133 nor current process has android.permission.STATUS_BAR. 請問有甚麼方法可以解決權限的問題嗎?或是還有甚麼方法可以阻止使用者使用STATUS_BAR呢?

    public void disableStatusBar() {
        try {
            @SuppressLint("WrongConstant")
            Object service = context.getSystemService("statusbar");
            Class<?> claz = Class.forName("android.app.StatusBarManager");
            Method disable = claz.getMethod("disable", new Class[]{int.class});
            disable.setAccessible(true);
            disable.invoke(service, DISABLE_EXPAND);
        } catch (Exception e) {
            System.out.println("e: " + e.toString());
            System.out.println(e.getCause());
            e.printStackTrace();
        }
    }
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-09-24 15:38:34

試試看這篇

這個我之前試過,因為StatusBarManager的disable為hide且UnsupportedAppUsage,所以改用Class.getMethod(),結果最後還是卡在android.permission.STATUS_BAR這一關...

0
barr
iT邦見習生 ‧ 2022-01-13 02:20:56

https://zhuanlan.zhihu.com/p/277342333
這篇是你要的,官方中文版可以放心看
不過我不確定還有沒有改,可以的話看一下英文版的
https://medium.com/androiddevelopers/gesture-navigation-going-edge-to-edge-812f62e4e83e
https://medium.com/androiddevelopers/gesture-navigation-handling-visual-overlaps-4aed565c134c

沒記錯的話應該是這樣
在Activity裡用這個,讓APP可以在繪製在系統欄上
不過單純沉浸模式不確定要不要用到這個
WindowCompat.setDecorFitsSystemWindows(window, false)

另一個是開啟沉浸模式
val controller = view.windowInsetsController

// 现在开始吸附式沉浸式体验 ...
controller.setSystemBarsBehavior(
BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
)

// 当我们想要隐藏系统栏
controller.hide(Type.systemBars())

官方文檔
https://developer.android.com/reference/android/view/WindowInsetsController#BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
API最好看一下,API30和API31都有改過
會有點麻煩 = =

我要發表回答

立即登入回答