如何以编程方式使用密码设置屏幕锁定?
Posted
技术标签:
【中文标题】如何以编程方式使用密码设置屏幕锁定?【英文标题】:How can i set up screen lock with a password programmatically? 【发布时间】:2012-04-01 05:43:11 【问题描述】:有没有人可以帮助我设置密码以锁定屏幕?谢谢
【问题讨论】:
【参考方案1】:在您的应用程序中使用此代码,它适用于我:
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName demoDeviceAdmin = new ComponentName(this, name of activity);
devicePolicyManager.setPasswordQuality(
demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 5);
boolean result = devicePolicyManager.resetPassword("123456",
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
Toast.makeText(this, "button_lock_password_device..."+result, Toast.LENGTH_LONG).show();
【讨论】:
什么是“活动名称”?哪个活动? 活动名称是什么? “活动名称”是继承自 DeviceAdminReceiver 的类的名称。【参考方案2】:检查这个。这对我有用。
LockScreenActivity.java
package com.kns;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LockScreenActivity extends Activity implements OnClickListener
private Button lock;
private Button disable;
private Button enable;
static final int RESULT_ENABLE = 1;
DevicePolicyManager deviceManger;
ActivityManager activityManager;
ComponentName compName;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
deviceManger = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager)getSystemService(
Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
setContentView(R.layout.main);
lock =(Button)findViewById(R.id.lock);
lock.setOnClickListener(this);
disable = (Button)findViewById(R.id.btnDisable);
enable =(Button)findViewById(R.id.btnEnable);
disable.setOnClickListener(this);
enable.setOnClickListener(this);
@Override
public void onClick(View v)
if(v == lock)
boolean active = deviceManger.isAdminActive(compName);
if (active)
deviceManger.lockNow();
if(v == enable)
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
if(v == disable)
deviceManger.removeActiveAdmin(compName);
updateButtonStates();
private void updateButtonStates()
boolean active = deviceManger.isAdminActive(compName);
if (active)
enable.setEnabled(false);
disable.setEnabled(true);
else
enable.setEnabled(true);
disable.setEnabled(false);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
switch (requestCode)
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK)
Log.i("DeviceAdminSample", "Admin enabled!");
else
Log.i("DeviceAdminSample", "Admin enable FAILED!");
return;
super.onActivityResult(requestCode, resultCode, data);
MyAdmin.java
package com.kns;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;
public class MyAdmin 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 msg)
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
@Override
public void onEnabled(Context context, Intent intent)
showToast(context, "Sample Device Admin: enabled");
@Override
public CharSequence onDisableRequested(Context context, Intent intent)
return "This is an optional message to warn the user about disabling.";
@Override
public void onDisabled(Context context, Intent intent)
showToast(context, "Sample Device Admin: disabled");
@Override
public void onPasswordChanged(Context context, Intent intent)
showToast(context, "Sample Device Admin: pw changed");
@Override
public void onPasswordFailed(Context context, Intent intent)
showToast(context, "Sample Device Admin: pw failed");
@Override
public void onPasswordSucceeded(Context context, Intent intent)
showToast(context, "Sample Device Admin: pw succeeded");
main.xml
<textview android:layout_ android:layout_ android:text="To Lock Screen First Enable it.." android:textsize="15sp"></textview>
<button android:id="@+id/lock" android:layout_ android:layout_ android:text="Lock The Phone"></button>
<button android:id="@+id/btnEnable" android:layout_ android:layout_ android:text="Enable"></button>
<button android:id="@+id/btnDisable" android:layout_ android:layout_ android:text="Disable"></button></linearlayout>
policies.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password>
<watch-login>
<reset-password>
<force-lock>
<wipe-data>
</wipe-data></force-lock></reset-password></watch-login></limit-password></uses-policies>
</device-admin>
您可以在布局或 xml 文件夹中拥有此策略.xml。您唯一需要注意的是 Androidmanifest 文件中的 metadata 标记。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kns" android:versioncode="1" android:versionname="1.0">
<uses-sdk android:minsdkversion="8">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LockScreenActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
</activity>
<receiver android:name=".MyAdmin" android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="@layout/policies">
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED">
</action></intent-filter>
</meta-data></receiver>
</application>
</uses-sdk></manifest>
在锁定屏幕之前,您需要启用管理员权限
那么你会得到这个..
启用后你会锁屏..像这样
【讨论】:
始终欢迎提供潜在解决方案的链接,但请add context around the link,以便您的其他用户知道它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。考虑到仅仅是指向外部站点的链接是Why and how are some answers deleted? 的一个可能原因。 感谢您对 SO 的贡献! 它在模拟器中工作。但它没有正确锁定。屏幕变黑,可以通过按返回选项返回应用程序。【参考方案3】:http://developer.android.com/guide/topics/admin/device-admin.html
private static DevicePolicyManager dpm =
(DevicePolicyManager)context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
private static ComponentName admin =
new ComponentName(context, DeviceAdminManager.class);
// add password policies you want
dpm.setPasswordQuality(admin, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
dpm.setPasswordMinimumLength(admin, 5);
**boolean result = dpm.resetPassword("newPassword", RESET_PASSWORD_WITHOUT_ENTRY);**
【讨论】:
【参考方案4】:查看此网站http://blog.stef.be/patternlock。在这个网站上,使用javascript、html和css实现的锁屏模式已经定义的很清楚了……全部代码开源了。希望对你有帮助
【讨论】:
【参考方案5】:Android N 不工作,See Here
注意:自 N 起,此 API 已被限制为非设备所有者和个人资料所有者的设备管理员。现在只能在当前未设置密码的情况下更改密码。当用户已解锁且没有受管理的个人资料时,设备所有者和个人资料所有者仍然可以执行此操作。
【讨论】:
以上是关于如何以编程方式使用密码设置屏幕锁定?的主要内容,如果未能解决你的问题,请参考以下文章