Android Lock Task Ui 功能
Posted 虫师魁拔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Lock Task Ui 功能相关的知识,希望对你有一定的参考价值。
android 的 Lock Task UI 功能试用于部分特定场景:全屏游戏、工厂测试等,屏蔽除 BACK 按键以外按键,屏蔽状态栏通知栏等。
使用时需要将应用设置为 DeviceOwner ,系统通过 DevicePolicyManager.setDeviceOwner 设置,或者通过 adb shell dpm set-active-admin pkg/cls
DevicePolicyManager 类主要相关方法:
setDeviceOwner
设置应用为设备管理者
setLockTaskFeatures
设置不同的 flag 来解除状态通知栏、按键的锁定
setLockTaskPackages
设置额外可以启动的应用包名
clearDeviceOwnerApp
清除设备管理应用
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import com.locktaskui.test.receiver.DeviceAdminTestReceiver;
import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_HOME;
import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS;
import static android.app.admin.DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW;
public class LockTaskUiTestActivity extends Activity implements View.OnClickListener
private static final String TAG = LockTaskUiTestActivity.class.getSimpleName();
private static final ComponentName ADMIN_RECEIVER = DeviceAdminTestReceiver.getReceiverComponentName();
;
private ActivityManager mAm;
private DevicePolicyManager mDpm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.locktask_layout);
findViewById(R.id.enable_noti).setOnClickListener(this);
findViewById(R.id.enable_home).setOnClickListener(this);
findViewById(R.id.enable_over).setOnClickListener(this);
findViewById(R.id.start_lock).setOnClickListener(this);
findViewById(R.id.stop_lock).setOnClickListener(this);
this.mDpm = (DevicePolicyManager) getSystemService(DevicePolicyManager.class);
private String getCurrentLauncherPackage()
@SuppressLint("WrongConstant")
ResolveInfo resolveInfo = getPackageManager().resolveActivity((new Intent("android.intent.action.MAIN")).addCategory("android.intent.category.HOME"), 65536);
if (resolveInfo != null)
ActivityInfo activityInfo = resolveInfo.activityInfo;
if (activityInfo != null)
return activityInfo.packageName;
return null;
@Override
public void onClick(View v)
switch (v.getId())
case R.id.enable_noti:
this.mDpm.setLockTaskFeatures(this.ADMIN_RECEIVER, LOCK_TASK_FEATURE_NOTIFICATIONS | LOCK_TASK_FEATURE_HOME);
this.mDpm.setLockTaskPackages(this.ADMIN_RECEIVER, new String[]getPackageName(), getCurrentLauncherPackage());
break;
case R.id.enable_home:
this.mDpm.setLockTaskFeatures(this.ADMIN_RECEIVER, LOCK_TASK_FEATURE_HOME);
this.mDpm.setLockTaskPackages(this.ADMIN_RECEIVER, new String[]getPackageName(), getCurrentLauncherPackage());
break;
case R.id.enable_over:
this.mDpm.setLockTaskFeatures(this.ADMIN_RECEIVER, LOCK_TASK_FEATURE_OVERVIEW | LOCK_TASK_FEATURE_HOME);
this.mDpm.setLockTaskPackages(this.ADMIN_RECEIVER, new String[]getPackageName(), getCurrentLauncherPackage());
break;
case R.id.start_lock:
this.mDpm.setLockTaskPackages(ADMIN_RECEIVER, new String[]"com.seuic.seuictest");
this.mDpm.setLockTaskFeatures(ADMIN_RECEIVER, 0);
startLockTask();
break;
case R.id.stop_lock:
stopLockTask();
LockTaskUiTestActivity.this.mDpm.setLockTaskFeatures(LockTaskUiTestActivity.ADMIN_RECEIVER, 0);
LockTaskUiTestActivity.this.mDpm.setLockTaskPackages(LockTaskUiTestActivity.ADMIN_RECEIVER, new String[0]);
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.clearDeviceOwnerApp(getPackageName());
break;
import android.app.admin.DeviceAdminReceiver;
import android.content.ComponentName;
public class DeviceAdminTestReceiver extends DeviceAdminReceiver
private static final ComponentName RECEIVER_COMPONENT_NAME = new ComponentName("com.locktaskui.test", "com.locktaskui.test.receiver.DeviceAdminTestReceiver");
public static ComponentName getReceiverComponentName()
return RECEIVER_COMPONENT_NAME;
xml 中需要配置如下内容。
<receiver
android:name=".receiver.DeviceAdminTestReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<?xml version="1.0" encoding="utf-8"?>
<device-admin
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
<disable-keyguard-features />
</uses-policies>
</device-admin>
以上是关于Android Lock Task Ui 功能的主要内容,如果未能解决你的问题,请参考以下文章