我想在没有任何用户请求的情况下激活startLockTask()。所以一开始我读了Device Administration 并创建小样本应用程序。结果我在设备管理员列表中有我的应用程序但是当我调用startLockTask()时它仍然不以静默模式执行。 当我试图这么称呼它时:
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
String[] packages = {this.getPackageName()};
myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, packages);
startLockTask();
我得到下一个例外:java.lang.SecurityException: Admin ComponentInfo{com.hssoft.deviceadmintest/com.hssoft.deviceadmintest.DeviceAdmin} does not own the device
所以我有设备管理员,但我不是设备所有者。我做错了什么,有人可以帮忙吗? 这是我的完整应用代码:
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hssoft.deviceadmintest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.hssoft.deviceadmintest.DeviceAdmin"
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.PROFILE_PROVISIONING_COMPLETE"/>
</intent-filter>
</receiver>
</application>
</manifest>
XML / 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/>
</uses-policies>
</device-admin>
DeviceAdmin.java:
package com.hssoft.deviceadmintest;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Log.i("Device Admin: ", "Enabled");
}
@Override
public String onDisableRequested(Context context, Intent intent) {
return "Admin disable requested";
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Log.i("Device Admin: ", "Disabled");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
Log.i("Device Admin: ", "Password changed");
}
}
MainActivity.java:
package com.hssoft.deviceadmintest;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
static final int ACTIVATION_REQUEST = 1;
ComponentName deviceAdminComponentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
startActivityForResult(intent, ACTIVATION_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Log.i("MainActivity", "Administration enabled!");
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
String mPackageName = this.getPackageName();
if (myDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, new String[]{mPackageName});
}
if (myDevicePolicyManager.isLockTaskPermitted(mPackageName)) {
startLockTask();
}
} else {
Log.i("MainActivity", "Administration enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
答案 0 :(得分:2)
您的应用程序必须是设备所有者才能按照您的意愿运行,如果您具有root访问权限,则可以运行此命令来实现该目标
adb shell dpm set-device-owner com.hssoft.deviceadmintest/.DeviceAdmin
this文章可以帮到你。