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

时间:2017-06-06 01:30:28

标签: android kiosk

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

我遇到的问题是我收到“没有活动的管理员”错误。我不知道如何让我活跃。有意打电话会提示这个提示吗?我直接从开发人员那里得到了说明,但遗漏了一些未在网站上公布的细节。

  

引起:java.lang.SecurityException:没有活动的管理员   ComponentInfo {android.example.stage / com.example.app.framework.utilities.kiosk.DeviceAdminReceiver}                                                                                    在android.os.Parcel.readException(Parcel.java:1693)                                                                                    在android.os.Parcel.readException(Parcel.java:1646)                                                                                    在   android.app.admin.IDevicePolicyManager $存根$ Proxy.setUserRestriction(IDevicePolicyManager.java:7977)                                                                                    在   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)                                                                                    在android.os.Looper.loop(Looper.java:154)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:6682)                                                                                    at java.lang.reflect.Method.invoke(Native Method)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

以下是我的实现方式。

在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);
    }

在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();
        }
    }

我的助手方法

@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);
        }
    }

我的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>

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>

1 个答案:

答案 0 :(得分:0)

  1. Create a device administration receiverMyDeviceAdminReceiver.kt 中:
class MyDeviceAdminReceiver : DeviceAdminReceiver()
  1. Define and declare your policy

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>

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>
  1. Make sure to log out of all accounts 位于“设置”>“设备帐户”中。

  2. Run the dpm command to set device owner

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

  1. Allowlist apps(这次是 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);
  1. Start lock task mode
val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
if (dpm.isLockTaskPermitted(packageName)) {
    startLockTask()
}