gpt4 book ai didi

android-studio - Android Studio、信息亭模式、单一用途设备、锁定任务模式

转载 作者:行者123 更新时间:2023-12-03 17:41:50 25 4
gpt4 key购买 nike

我正在尝试创建一个单一用途的应用程序。
所以我创建了一个 BaseActivity,我的所有事件都继承自它。

它看起来像

public class LockDeviceActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

private void startLock() {


if(mDevicePolicyManager.isLockTaskPermitted(getPackageName())) {
/**
* If lock task is permitted, we can lock the task. We can use an external DPM like
* TestDPC provided by Google to manage lock task list.
*
* If the lock is obtained using TestDPC, features like status bar, home button, recent
* apps, etc is disabled.
*
* To unlock we can programatically call stopLockTask() when users taps a button. But
* in practice this should be done using a separate admin console or Confirm Credential.
*
* For API 23+ you can check if the lock is active by checking if
* activityManager.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE
*/
Log.d(TAG, "startLock() called");
this.startLockTask();
} else {
/**
* The device is not whitelisted.
*/
Toast.makeText(this, "The app is not whitelisted for lock", Toast.LENGTH_SHORT).show();
// Timber.d("The app is not whitelisted for lock task");


/**
* We can still pin the app but it will not be locked.
*
* We can simply unlock by pressing recent and back button together.
*
* Unlocking by calling stopLockTask() on button click can be achieved as well.
*/
// Timber.d("just pinning the app");
this.startLockTask();
}

}

所以当我第一次进入应用程序时,我可以看到一些固定的消息,我没问题。
问题是,当我从一个事件到另一个包含 Fragment 的事件执行意图时,我得到以下图像:

Annyoing Message

另外,我收到了一些系统 toast 消息:
“该应用程序未列入锁定白名单”

我怎样才能避免这种行为?
谢谢你们 。

最佳答案

1) 将设备重置为出厂默认设置
2) 跳过账户注册
3)安装应用程序然后关闭它
4) 运行 adb shell dpm set-device-owner <app-package-name>/.<AppAdminReceiver>5)确保你得到一个成功的结果。
6) 重新启动应用程序。

必须满足以下条件:
AndroidManifest.xmlapplication标签

<activity
android:name=".AppActivity"
android:label="Locked Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>


<receiver
android:name=".AppAdminReceiver"
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_receiver" />
<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>


创建一个名为 AppAdminReceiver.java 的新 Java 类(留空就好)

public class AppAdminReceiver extends android.app.admin.DeviceAdminReceiver {

}


添加 device_admin_receiver.xml/res/xml目录

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<disable-keyguard-features/>
</uses-policies>
</device-admin>


AppActivity.java调用 startLockTask()stopLockTask()
private ComponentName mAdminComponentName;
private DevicePolicyManager mDevicePolicyManager;
...

... onCreate(){

// Retrieve Device Policy Manager so that we can check whether we can
// lock to screen later
mAdminComponentName = new ComponentName(this,AppAdminReceiver.class);
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if(mDevicePolicyManager.isDeviceOwnerApp(getPackageName())){
// App is whitelisted
setDefaultCosuPolicies(true);
}
else {
// did you provision the app using <adb shell dpm set-device-owner ...> ?
}
}

... onStart(){
// Consider locking your app here or by some other mechanism
// Active Manager is supported on Android M
if(mDevicePolicyManager.isLockTaskPermitted(this.getPackageName())){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_NONE) {
setDefaultCosuPolicies(true);
startLockTask();
}
}
}


... unlockApp(){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

if (am.getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED) {
stopLockTask();
}
setDefaultCosuPolicies(false);

}

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(), AppActivity.class.getName()));
} else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities(mAdminComponentName, getPackageName());
}
}

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

private void enableStayOnWhilePluggedIn(boolean enabled){
if (enabled) {
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,Settings.Global.STAY_ON_WHILE_PLUGGED_IN,Integer.toString(BatteryManager.BATTERY_PLUGGED_AC| BatteryManager.BATTERY_PLUGGED_USB| BatteryManager.BATTERY_PLUGGED_WIRELESS));
} else {
mDevicePolicyManager.setGlobalSetting(mAdminComponentName,Settings.Global.STAY_ON_WHILE_PLUGGED_IN,"0");
}
}
}


Similar tutorial on setting up kiosk mode

关于android-studio - Android Studio、信息亭模式、单一用途设备、锁定任务模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com