防止用户未经许可退出应用程序或重新启动设备



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

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

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

您想要设置一个专用设备。正如@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)
)
}
}

一旦你的应用程序加载,从外壳将其设置为设备所有者:

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

相关内容

最新更新