Android设备策略管理器不工作

时间:2015-08-25 15:11:41

标签: java android xml lockscreen device-policy-manager

我正在尝试创建一个简单的应用程序,以便在接听来电时锁定设备的屏幕。为此,我在this教程的帮助下成为了一个自定义设备管理员。但是,当我单击应用程序中的“启用”按钮时,设备策略管理器活动根本无法打开!我在logcat窗口中输出结果,表明我的ComponentName无效。但是,我的项目中没有发现任何问题!

MainActivity.java

package com.swap.delay;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

DevicePolicyManager devicePolicyManager;
ActivityManager activityManager;
ComponentName componentName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    componentName = new ComponentName(this, LockAdmin.class);

}

public void OnClickActivate(View view) {

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Lock Admin by Swap");

    startActivityForResult(intent, 1);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
        Toast.makeText(getBaseContext(), "Lock Admin by Swap added successfully", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getBaseContext(), "Failed!", Toast.LENGTH_SHORT).show();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

LockAdmin.java

package com.swap.delay;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

public class LockAdmin extends DeviceAdminReceiver{

static SharedPreferences getSamplePreferences(Context context) {
    return context.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0);
}

static String PREF_PASSWORD_QUALITY = "password_quality";
static String PREF_PASSWORD_LENGTH = "password_length";
static String PREF_MAX_FAILED_PW = "max_failed_pw";

void showToast(Context context, CharSequence message) {
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent) {
    showToast(context, "Lock Admin Enabled");
}

@Override
public void onPasswordFailed(Context context, Intent intent) {
    showToast(context, "Lock Admin: Wrong Password");
}

@Override
public void onPasswordSucceeded(Context context, Intent intent) {
    //showToast(context, "Sample Device Admin: pw succeeded");
}

}

policies.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/auto">
<uses-policies>
    <force-lock>
    </force-lock>
</uses-policies>
</device-admin>

Receiver(添加到manifest.xml)

<receiver android:name=".LockAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin" android:resource="@xml/policies">
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED">
                </action></intent-filter>
        </meta-data></receiver>

Logcat输出 -

08-25 12:52:40.103     652-1906/? I/ActivityManager﹕ START u0 {act=android.app.action.ADD_DEVICE_ADMIN cmp=com.android.settings/.DeviceAdminAdd (has extras)} from uid 10172 on display 0
08-25 12:52:40.162     652-1070/? I/ActivityManager﹕ Start proc com.android.settings for activity com.android.settings/.DeviceAdminAdd: pid=6313 uid=1000 gids={41000, 9997, 3002, 3001, 3003, 1028, 1015, 1000, 1021, 3004, 3005, 3009, 1023, 1010, 1007} abi=arm64-v8a
08-25 12:52:40.332    6313-6313/? W/DeviceAdminAdd﹕ Request to add invalid device admin: ComponentInfo{com.swap.delay/com.swap.delay.LockAdmin}

P.S。 - 我在基于AOSP KitKat(4.4.2)和另一个CyanogenOS 12.0(5.0.2)的两个不同设备上测试了应用程序,但它们都无法正常工作。

0 个答案:

没有答案