如何创建COSU应用程序?无活动管理员错误

por*_*der 5 android kiosk

我目前正在尝试创建COSU应用。我想将用户锁定在应用程序中,并禁用所有功能,例如主页按钮,菜单按钮,后退按钮,工具栏等。我在这里遵循了指南:https : //developer.android.com/work/cosu.html#cosu-solutions

我遇到的问题是我收到“没有活动的管理员”错误。我不知道如何使我活跃。是否有通话意图会弹出此提示?我直接按照开发人员的指示进行操作,但是缺少一些未发布在网站上的详细信息。

造成原因:java.lang.SecurityException:在android.os.Parcel.readException(Parcel.java:1693)处没有活动的管理员ComponentInfo {android.example.stage / com.example.app.framework.utilities.kiosk.DeviceAdminReceiver} android.app.admin.IDevicePolicyManager $ Stub $ Proxy.setUserRestriction(IDevicePolicyManager.java:7977)上的android.os.Parcel.readException(Parcel.java:1646),android.app.admin.DevicePolicyManager.addUserRestriction(DevicePolicyManager.java:6296),位于android.example.activity.RegistrationActivity.setUserRestriction(RegistrationActivity.java:320),位于android.example.activity.RegistrationActivity.setDefaultCosuPolicies(RegistrationActivity.java:274),位于android.example.activity.RegistrationActivity.onCreate(RegistrationActivity.java) :110)在android.app.Activity.performCreate(Activity.java:6942)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)在android.app.ActivityThread.-wrap14(ActivityThread.java)在android.app .ActivityThread $ H.handleMessage(ActivityThread.java:1631)在android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6682)at com.android的java.lang.reflect.Method.invoke(Native Method) .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)154),位于android.app.ActivityThread.main(ActivityThread.java:6682),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:的java.lang.reflect.Method.invoke(本机方法)处。 1520),位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)154),位于android.app.ActivityThread.main(ActivityThread.java:6682),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:的java.lang.reflect.Method.invoke(本机方法)处。 1520),位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)处com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)处调用(本机方法)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)处com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)处调用(本机方法)

这是我的实现。

在OnCreate()中

    mAdminComponentName = DeviceAdminReceiver.getComponentName(this);
    mDevicePolicyManager = (DevicePolicyManager) getSystemService(
            Context.DEVICE_POLICY_SERVICE);
    mPackageManager = getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        setDefaultCosuPolicies(true);
    }
Run Code Online (Sandbox Code Playgroud)

在onStart()中

    // start lock task mode if it's not already active
    ActivityManager am = (ActivityManager) getSystemService(
            Context.ACTIVITY_SERVICE);
    // ActivityManager.getLockTaskModeState api is not available in pre-M.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (!am.isInLockTaskMode()) {
                startLockTask();
            }
        }
    } else {
        if (am.getLockTaskModeState() ==
                ActivityManager.LOCK_TASK_MODE_NONE) {
            startLockTask();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的助手方法

@TargetApi(Build.VERSION_CODES.M)
 private void setDefaultCosuPolicies(boolean active) {
     // set user restrictions
     setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
     setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
     setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
     setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
     setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);

     // disable keyguard and status bar
     mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
     mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);

     // enable STAY_ON_WHILE_PLUGGED_IN
     enableStayOnWhilePluggedIn(active);

     // set System Update policy

     if (active){
         mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
                 SystemUpdatePolicy.createWindowedInstallPolicy(60,120));
     } else {
         mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, null);
     }

     // set this Activity as a lock task package

     mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
             active ? new String[]{getPackageName()} : new String[]{});

     IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
     intentFilter.addCategory(Intent.CATEGORY_HOME);
     intentFilter.addCategory(Intent.CATEGORY_DEFAULT);

     if (active) {
         // set Cosu activity as home intent receiver so that it is started
         // on reboot
         mDevicePolicyManager.addPersistentPreferredActivity(
                 mAdminComponentName, intentFilter, new ComponentName(
                         getPackageName(), RegistrationActivity.class.getName()));
     } else {
         mDevicePolicyManager.clearPackagePersistentPreferredActivities(
                 mAdminComponentName, getPackageName());
     }
 }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setUserRestriction(String restriction, boolean disallow) {
        if (disallow) {
            mDevicePolicyManager.addUserRestriction(mAdminComponentName,
                    restriction);
        } else {
            mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
                    restriction);
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void enableStayOnWhilePluggedIn(boolean enabled) {
        if (enabled) {
            mDevicePolicyManager.setGlobalSetting(
                    mAdminComponentName,
                    Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
                    Battery_PLUGGED_ANY);
        } else {
            mDevicePolicyManager.setGlobalSetting(
                    mAdminComponentName,
                    Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的device_admin.xml

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
    <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>
Run Code Online (Sandbox Code Playgroud)

我在AndroidManifest.xml中的接收器

    <receiver
        android:name="com.example.app.framework.utilities.kiosk.DeviceAdminReceiver"
        android:description="@string/app_name"
        android:label="@string/app_name"
        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.intent.action.DEVICE_ADMIN_ENABLED"/>
            <action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

Ada*_*hns 1

  1. 在以下位置创建设备管理接收器MyDeviceAdminReceiver.kt
class MyDeviceAdminReceiver : DeviceAdminReceiver()
Run Code Online (Sandbox Code Playgroud)
  1. 定义并声明您的政策

res/xml/device_admin.xml:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <!-- Put whatever policies you need below -->
        <limit-password />
    </uses-policies>
</device-admin>
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml:

<receiver android:name=".MyDeviceAdminReceiver"
    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>
Run Code Online (Sandbox Code Playgroud)
  1. 确保在设备上的“设置”>“帐户”中注销所有帐户

  2. 运行 dpm 命令设置设备所有者

adb shell dpm set-device-owner com.example.myapp/.MyDeviceAdminReceiver

  1. 白名单应用程序(这次是 Java,而不是 Kotlin):
String[] ALLOWED_PACKAGES = {getPackageName()};
DevicePolicyManager dpm = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminName = new ComponentName(getApplicationContext(), MyDeviceAdminReceiver.class);
dpm.setLockTaskPackages(adminName, ALLOWED_PACKAGES);
Run Code Online (Sandbox Code Playgroud)
  1. 启动锁定任务模式
val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
if (dpm.isLockTaskPermitted(packageName)) {
    startLockTask()
}
Run Code Online (Sandbox Code Playgroud)