我正在尝试为Android设置锁定屏幕,版本大于蜂窝。我一个月以来一直在寻找一种有效的解决方案但是没有成功。我将具体说明我的情况。
我真的很感激工作代码。
提前致谢。
答案 0 :(得分:1)
让我们尝试一下它不会让状态栏完全不可触及,但它会阻止它被拖动,首先在mainfest文件中为EXPAND_STATUS_BAR提供权限
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
在您的活动中声明这些变量:
// This will keep track of activity's window focus
boolean currentFocus;
// This will keep track of activity's foreground/background status
boolean isPaused;
Handler collapseNotificationHandler;
它会要求您覆盖以下方法,覆盖它
@Override
public void onWindowFocusChanged(boolean hasFocus) {
currentFocus = hasFocus;
if (!hasFocus) {
// Method that handles loss of window focus
collapse_now();
}
}
定义collapse_now方法
public void collapse_now() {
// Initialization for 'collapseNotificationHandler'
if (collapseNotificationHandler == null) {
collapseNotificationHandler = new Handler();
}
// If window focus has been lost && activity is not in a paused state
// Its a valid check because showing of notification panel
// steals the focus from current activity's window, but does not
// 'pause' the activity
if (!currentFocus && !isPaused) {
// Post a Runnable with some delay - currently set to 300 ms
collapseNotificationHandler.postDelayed(new Runnable() {
@Override
public void run() {
// Use reflection to trigger a method from 'StatusBarManager'
Object statusBarService = getSystemService("statusbar");
Class<?> statusBarManager = null;
try {
statusBarManager = Class.forName("android.app.StatusBarManager");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method collapseStatusBar = null;
try {
// Prior to API 17, the method to call is 'collapse()'
// API 17 onwards, the method to call is `collapsePanels()`
if (Build.VERSION.SDK_INT > 16) {
collapseStatusBar = statusbarManager.getMethod("collapsePanels");
} else {
collapseStatusBar = statusbarManager.getMethod("collapse");
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
collapseStatusBar.setAccessible(true);
try {
collapseStatusBar.invoke(statusBarService);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// Check if the window focus has been returned
// If it hasn't been returned, post this Runnable again
// Currently, the delay is 100 ms. You can change this
// value to suit your needs.
if (!currentFocus && !isPaused) {
collapseNotificationHandler.postDelayed(this, 100L);
}
}
}, 300L);
}
}
处理活动的暂停状态
@Override
protected void onPause() {
super.onPause();
// Activity's been paused
isPaused = true;
}
@Override
protected void onResume() {
super.onResume();
// Activity's been resumed
isPaused = false;
}