使用DevicePolicyManager锁定屏幕

时间:2015-01-20 15:20:15

标签: android

我知道这不是第一次提出这个问题,但我无法找到解决问题的方法。

以下是一些代码:

MainActivity.class:

package com.fromscratch.aside;


import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

DevicePolicyManager mDPM;
ComponentName mAdminName;
protected static final int REQUEST_ENABLE = 0;
private Button bLockButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAdminName = new ComponentName(MainActivity.this,MyAdmin.class);
    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    bLockButton = (Button)findViewById(R.id.lock_button);
    bLockButton.setOnClickListener(this);
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(REQUEST_ENABLE == requestCode){
        System.out.println(mDPM.isAdminActive(mAdminName));
            super.onActivityResult(requestCode,resultCode,data);
    }
}

@Override
public void onClick(View v) {
    switch(v.getId()){
        case (R.id.lock_button):
            lockButtonClicked();
    }
}

private void lockButtonClicked(){
    if(!mDPM.isAdminActive(mAdminName)){

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName);
        startActivityForResult(intent,REQUEST_ENABLE);
    }
    else{
        mDPM.lockNow();
    }
}

public class MyAdmin extends DeviceAdminReceiver{

}
}

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.fromscratch.aside" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".MainActivity$MyAdmin"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.aSide"
                android:resource="@xml/my_admin"/>
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

my_admin.xml:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
    <limit-password/>
    <watch-login/>
    <reset-password/>
    <wipe-data/>
    <force-lock/>
</uses-policies>

阅读和阅读几个小时后,我不知道这段代码有什么问题。 但我认为主要的问题是我没有得到&#34;接受管理员权限对话框&#34;当我点击我的应用程序中的锁定按钮时。

我厌倦了商店中的antoher应用程序,它也可以锁定屏幕(任务器),以确保我的设备没有问题(galaxy note 4)。 - &GT;它工作(管理员权限对话框出现 - &gt; Ok - &gt;屏幕被锁定)

所以也许你们有任何想法,我在这里失踪了。

提前致谢

1 个答案:

答案 0 :(得分:6)

我看到几个问题:

  1. 我没有看到DeviceAdminReceiver实现。

  2. 您没有以任何方式启动设备管理设置。

  3. 我强烈建议您阅读本文档:

    http://developer.android.com/guide/topics/admin/device-admin.html

    以下是从我正在处理的应用程序锁定屏幕的快速示例:

    private void lockScreen() {
        PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
        if (pm.isScreenOn()) {
            DevicePolicyManager policy = (DevicePolicyManager)
                    getSystemService(Context.DEVICE_POLICY_SERVICE);
            try {
                policy.lockNow();
            } catch (SecurityException ex) {
                Toast.makeText(
                        this, 
                        "You must enable this app as a device administrator\n\n" +
                        "Please enable it and press back button to return here.",
                        Toast.LENGTH_LONG).show();
                    ComponentName admin = new ComponentName(context, AdminReceiver.class);
                    Intent intent = new Intent(
                        DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).putExtra(
                            DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
                    context.startActivity(intent);
            }           
        }
    }
    

    AdminReceiver.java:

    import android.app.admin.DeviceAdminReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.content.LocalBroadcastManager;
    
    public class AdminReceiver extends DeviceAdminReceiver {
        public static final String ACTION_DISABLED = "device_admin_action_disabled";
        public static final String ACTION_ENABLED = "device_admin_action_enabled";
    
        @Override
        public void onDisabled(Context context, Intent intent) {
            super.onDisabled(context, intent);
            LocalBroadcastManager.getInstance(context).sendBroadcast(
                new Intent(ACTION_DISABLED));
        }
        @Override
        public void onEnabled(Context context, Intent intent) {
            super.onEnabled(context, intent);
            LocalBroadcastManager.getInstance(context).sendBroadcast(
                new Intent(ACTION_ENABLED));
        }
    }
    

    device_admin.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <device-admin xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-policies>
            <force-lock />
        </uses-policies>
    </device-admin>
    

    并将其添加到您的清单中:

        <receiver
            android:name="com.example.lockthescreen.receivers.AdminReceiver"
            android:label="@string/device_admin"
            android:description="@string/device_admin_description"
            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.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>
    

    希望这可以帮到你。

    此致

    凯尔