gpt4 book ai didi

android - 使用 DevicePolicyManager 锁定屏幕

转载 作者:行者123 更新时间:2023-11-29 14:37:31 24 4
gpt4 key购买 nike

我知道这不是第一次有人问这个问题,但我找不到任何解决方案。

这是给你们的一些代码:

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>

我的管理员.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>

读了几个小时后,我不知道这段代码有什么问题。但我认为主要问题是,当我在我的应用程序中单击锁定按钮时,我没有看到“接受管理员权限对话框”。

我厌倦了商店里的 antoher 应用程序,它也可以锁定屏幕 (tasker),以确保我的设备 (galaxy note 4) 没有问题。--> 成功了(出现管理员权限对话框 --> 确定 --> 屏幕被锁定)

所以也许你们知道我在这里缺少什么。

提前致谢

最佳答案

我看到几个问题:

  1. 我没有看到 DeviceAdminReceiver 实现。

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

我强烈建议您阅读此文档:

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>

并将其添加到您的 list 中:

    <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>

希望这对您有所帮助。

问候,

凯尔

关于android - 使用 DevicePolicyManager 锁定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28048960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com