在Android Developers之后,我创建了一个简单的管理应用程序,要求用户启用系统管理应用程序。我想锁定该应用程序,以使用户一旦同意允许系统应用程序(对信息亭设备有用)就无法退出该应用程序。
我没有看到以上述方式锁定应用程序的API。这是我的代码:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private ComponentName mDeviceAdmin;
private DevicePolicyManager mDPM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.enable_admin);
// Prepare to work with the DPM
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdmin = new ComponentName(this, MyDeviceAdminReceiver.class);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Launch the activity to have the user enable our admin.
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"We need admin access");
startActivityForResult(intent, 100);
// return false - don't update checkbox until we're really active
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode == 100) {
mDPM.lockNow();
}
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
return super.onKeyLongPress(keyCode, event);
}
}
呼叫lockNow()
会立即锁定设备并关闭显示器(距离我想要的远)。如何为我的应用实现信息亭锁定?
假设用户启用了我的应用程序作为系统管理员。然后,如何截获“主页”按钮和“设置”(在遥控器上)之类的系统键,以防止用户退出应用程序?
答案 0 :(得分:0)
您可以通过以下代码实现
String[] packages=new String[1];
packages[0]="yourpackagename";
devicePolicyManager.isLockTaskPermitted(packages[0]);
startLockTask();//enabling kiosk mode