【问题标题】:Prevent user from exiting application or rebooting the device without permission防止用户未经许可退出应用程序或重启设备
【发布时间】:2021-11-23 15:15:13
【问题描述】:

我正在为东京的一家酒吧开发一个在平板电脑上运行的 Android 应用程序。

一切进展顺利,但我遇到了一个问题,当客户使用平板电脑时,他们可以通过按下硬按钮退出我的应用程序并重新启动设备,而我的客户不允许他们这样做,并且要求我配置我的应用程序以防止这种情况发生。

我研究了很多,但似乎没有明确的答案。任何建议将不胜感激,谢谢

【问题讨论】:

  • 您想要的是“kiosk 模式”,但不知道您的应用程序正在部署什么平板电脑,但是我不能说该怎么做。它通常在操作系统级别配置为将设备锁定到特定应用程序。

标签: android android-studio kotlin


【解决方案1】:

您想设置一个Dedicated Device。正如@DevWithZachary 所提到的,这曾经被称为“信息亭模式”。

这是我为我的设备实现它的方式:

将设备管理员接收器添加到您的清单中:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">

<application
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name">

    <!--  other stuff... -->

    <receiver
        android:name=".YourDeviceAdminReceiver"
        android:description="@string/app_name"
        android:exported="true"
        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_receiver" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            <action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE"/>
        </intent-filter>
    </receiver>
</application>

实现一个 DeviceAdminReceiver:

package com.example.yourapp

import android.app.admin.DeviceAdminReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.UserManager
import android.provider.Settings
import android.util.Log

class YourDeviceAdminReceiver : DeviceAdminReceiver() {
    val tag = "YourDeviceAdminReceiver"

    override fun onEnabled(context: Context, intent: Intent) {
        super.onEnabled(context, intent)
        lockDownApp(context)
        Log.i(tag, "*** Device Admin Enabled ***")
    }

    private fun lockDownApp(context: Context) {
        val devicePolicyManager = getManager(context)
        val adminComponentName = ComponentName(context.applicationContext, this::class.java.name)
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_SAFE_BOOT)
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_FACTORY_RESET)
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_ADD_USER)
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_SYSTEM_ERROR_DIALOGS)
        // DISALLOW_MOUNT_PHYSICAL_MEDIA prevents USB or other storage devices from being mounted on the generator.
        // DISALLOW_USB_FILE_TRANSFER prevents the generator from being either the USB host or device when transferring files.
        // Usually, DISALLOW_USB_FILE_TRANSFER can be considered a superset that includes DISALLOW_MOUNT_PHYSICAL_MEDIA.
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA)
        devicePolicyManager.addUserRestriction(adminComponentName, UserManager.DISALLOW_USB_FILE_TRANSFER)
        devicePolicyManager.setKeyguardDisabled(adminComponentName, true)
        devicePolicyManager.setStatusBarDisabled(adminComponentName, true)
        devicePolicyManager.setGlobalSetting(adminComponentName, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
            BatteryManager.BATTERY_PLUGGED_AC.toString())
        devicePolicyManager.setLockTaskPackages(adminComponentName, arrayOf(context.packageName))

        // set activity as home intent receiver so that it is started on reboot
        val intentFilter = IntentFilter(Intent.ACTION_MAIN)
        intentFilter.addCategory(Intent.CATEGORY_HOME)
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT)
        devicePolicyManager.addPersistentPreferredActivity(adminComponentName, intentFilter,
            ComponentName(context.packageName, YourActivity::class.java.name)
        )
    }
}

加载您的应用后,从 shell 将其设置为设备所有者:

dpm set-device-owner com.example.yourapp/.YourDeviceAdminReceiver

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多