锁屏与DevicePolicyManager
我知道它不是第一次这个问题是问,但我无法找到我的问题的任何解决方案。
I know its not the first time this question is asked, but i couldnt find any solution for my problem.
下面是一些code为你们:
Here is some code for you guys:
MainActivity.class:
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中
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:
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>
阅读和阅读小时后,我没有什么线索是不对的code。
但我认为主要的问题是,我不明白的接受管理权限对话框当我在我的应用程序点击锁定按钮。
After reading and reading for hours, i dont have a clue what is wrong with this code. But i think the main Problem is that i dont get the "accept admin permission dialog" when i click the lock button in my application.
我厌倦了这家店也是能够锁定屏幕(塔斯克)antoher应用程序,以确保它与我的设备(GALAXY NOTE 4)没有问题。
- >它的工作(管理员权限对话框出现 - >确定 - >屏幕锁定)
I tired antoher app from the store which is also able to lock the screen (tasker), to make sure that it is no problem with my device (galaxy note 4). --> It worked (Admin permission dialog comes up --> Ok --> screen is locked)
因此,也许你们有任何想法IAM在这里失踪。
So maybe you guys have any ideas what iam missing here.
在此先感谢
我看到几个问题:
-
我没有看到一个DeviceAdminReceiver实现。
I don't see a DeviceAdminReceiver implementation.
您是不以任何方式启动它的设备管理设置。
You're not launching it the device administration settings in any way.
我会强烈建议你读这本文件:
I would highly recommend you read this this document:
http://developer.android.com/guide/topics/管理/设备admin.html
下面是从一个应用程序,我在一个点上的工作锁定屏幕的快速示例:
Here is a quick sample on locking the screen from an app I was working on at one point:
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:
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:
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>
这应该可以帮助你。
Hopefully this should help you out.
问候,
凯尔