说明:屏幕固定是android自带的功能,在需要固定显示app,不允许切换其他app的时候使用。
业务需求:程序升级过程中,禁止退出后台界面。
android版本:android 11
- AndroidManifest.xml需要声明权限
<uses-permission android:name="android.permission.MANAGE_ACTIVITY_STACKS" />
- 检查运行时权限
int granted = checkSelfPermission("android.permission.MANAGE_ACTIVITY_STACKS");
if (granted != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{"android.permission.MANAGE_ACTIVITY_STACKS"}, id);
}
- 执行屏幕固定功能
int taskId = getTaskId();
try {
ActivityTaskManager.getService().startSystemLockTaskMode(taskId);
} catch (RemoteException e) {
}
- 关闭屏幕固定功能
try {
ActivityTaskManager.getService().stopSystemLockTaskMode();
} catch (RemoteException e) {
}