最近有朋友问屏幕锁定的问题,自己也在学习,网上找了下也没太详细的例子,看的资料书上也没有有关屏幕锁定程序的介绍,下个小决心,自己照着官方文档学习下,现在做好了,废话不多说,先发下截图,看下效果,需要注意的地方会加注释,有问题的朋友可以直接留言,我们共同学习交流,共同提高进步!直接看效果图:
一:未设置密码时进入系统设置的效果图如下:
二:设置密码方式预览:
三:密码解密效果图
四:九宫格解密时的效果图
下面来简单的看下源码吧,此处讲下,这个小DEMO也是临时学习下的,有讲的不明白的地方请朋友直接批评指出,有错肯定改的,吼吼看代码:
主控制类:
package com.xiaoma.policymanager.demo; import androID.app.Activity; import androID.app.admin.DevicePolicyManager; import androID.content.Intent; import androID.content.SharedPreferences; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.Widget.AdapterVIEw; import androID.Widget.ArrayAdapter; import androID.Widget.button; import androID.Widget.EditText; import androID.Widget.linearLayout; import androID.Widget.Spinner; import androID.Widget.TextVIEw; /** * @Title: PolicySetupActivity.java * @Package com.xiaoma.policymanager.demo * @Description: 屏幕锁实现测试 * @author MZH */ public class PolicySetupActivity extends Activity { private static final int REQ_ACTIVATE_DEVICE_admin = 10; private static final String SCREEN_ID_KEY = "LAYOUT_ID"; /**存储配置信息的SP文件名*/ private static final String APP_PREF = "APP_PREF"; /**有未知屏幕ID时返回此标识*/ private static final int UNKNowN_SCREEN_ID = -1; /**声明组件*/ private Spinner mPasswordQualityinputFIEld; private EditText mPasswordLengthinputFIEld; private EditText mPasswordMinUppercaseinputFIEld; /**获取配置 *** 作类对象 */ private Policy mPolicy; /**当前屏幕ID*/ private int mCurrentScreenID; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPolicy = new Policy(this); } @OverrIDe protected voID onResume() { super.onResume(); /**获取本地SP文件中屏幕锁的配置信息*/ SharedPreferences prefs = getSharedPreferences(APP_PREF, MODE_PRIVATE); final int savedScreenID = prefs.getInt(SCREEN_ID_KEY, UNKNowN_SCREEN_ID); /**如果获取到的ID为空,则跳转到设置页*/ if (savedScreenID == UNKNowN_SCREEN_ID || !mPolicy.isadminActive()) { setScreenContent(R.layout.activity_policy_setup); /**跳转到配置详情页*/ } else { setScreenContent(savedScreenID); } } /** * 设置屏幕方法实现,详细的可以自己看下,我也是边参照官方文档,边学习的 * 有好想法有问题的朋友们可以留言.我们一起学习 * @param screenID */ private voID setScreenContent(final int screenID) { /**记录当前屏幕ID,并预先存入本地SP配置文件中*/ mCurrentScreenID = screenID; setContentVIEw(mCurrentScreenID); getSharedPreferences(APP_PREF, MODE_PRIVATE).edit().putInt( SCREEN_ID_KEY, mCurrentScreenID).commit(); switch (mCurrentScreenID) { case R.layout.activity_policy_setup: initPolicySetupScreen(); initNavigation(); break; case R.layout.activity_vIEw_policy: initVIEwPolicyScreen(); initNavigation(); break; } } /** * 关闭页面时将当前配置写入本地SP文件中 */ @OverrIDe protected voID onPause() { super.onPause(); if (mCurrentScreenID == R.layout.activity_policy_setup) writePolicy(); } @OverrIDe protected voID onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQ_ACTIVATE_DEVICE_admin && resultCode == RESulT_OK) { // User just activated the application as a device administrator. setScreenContent(mCurrentScreenID); } else { super.onActivityResult(requestCode, resultCode, data); } } /** * 重载返回键方法,如果已当前屏幕为已设置密码页,则展示详情 */ @OverrIDe public voID onBackpressed() { if (mCurrentScreenID == R.layout.activity_vIEw_policy) { setScreenContent(R.layout.activity_policy_setup); return; } super.onBackpressed(); } /** * 初始化化设置页 */ private voID initPolicySetupScreen() { mPasswordQualityinputFIEld = (Spinner) findVIEwByID(R.ID.policy_password_quality); mPasswordLengthinputFIEld = (EditText) findVIEwByID(R.ID.policy_password_length); mPasswordMinUppercaseinputFIEld = (EditText) findVIEwByID(R.ID.policy_password_uppercase); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromresource(this, R.array.password_types, androID.R.layout.simple_spinner_item); adapter.setDropDownVIEwResource(androID.R.layout.simple_spinner_dropdown_item); mPasswordQualityinputFIEld.setAdapter(adapter); mPasswordQualityinputFIEld.setonItemSelectedListener( new AdapterVIEw.OnItemSelectedListener() { public voID onItemSelected(AdapterVIEw<?> parent, VIEw vIEw, int pos, long ID) { linearLayout passwordMinUppercaseVIEw = (linearLayout) findVIEwByID(R.ID.password_uppercase_vIEw); // The minimum number of upper case fIEld is only applicable for password // qualitIEs: Alpha, Alphanumeric, or complex. if (pos > 2) passwordMinUppercaseVIEw.setVisibility(VIEw.VISIBLE); else passwordMinUppercaseVIEw.setVisibility(VIEw.GONE); } public voID onnothingSelected(AdapterVIEw<?> parent) {} }); /**阅读先前配置的相关信息*/ mPolicy.readFromLocal(); mPasswordQualityinputFIEld.setSelection(mPolicy.getpasswordQuality()); if (mPolicy.getpasswordLength() > 0) { mPasswordLengthinputFIEld.setText(String.valueOf(mPolicy.getpasswordLength())); } else { mPasswordLengthinputFIEld.setText(""); } if (mPolicy.getpasswordMinUpperCase() > 0) { mPasswordMinUppercaseinputFIEld.setText( String.valueOf(mPolicy.getpasswordMinUpperCase())); } else { mPasswordMinUppercaseinputFIEld.setText(""); } } /** * 初始化查看详情页 */ private voID initVIEwPolicyScreen() { TextVIEw passwordQualityVIEw = (TextVIEw) findVIEwByID(R.ID.policy_password_quality); TextVIEw passwordLengthVIEw = (TextVIEw) findVIEwByID(R.ID.policy_password_length); // Read prevIoUsly saved policy and populate on the UI. mPolicy.readFromLocal(); int passwordQualitySelection = mPolicy.getpasswordQuality(); passwordQualityVIEw.setText( getResources().getStringArray(R.array.password_types)[passwordQualitySelection]); passwordLengthVIEw.setText(String.valueOf(mPolicy.getpasswordLength())); if (passwordQualitySelection > 2) { linearLayout passwordMinUppercaseVIEw = (linearLayout) findVIEwByID(R.ID.password_uppercase_vIEw); passwordMinUppercaseVIEw.setVisibility(VIEw.VISIBLE); ((TextVIEw) findVIEwByID(R.ID.policy_password_uppercase)).setText( String.valueOf(mPolicy.getpasswordMinUpperCase())); } } /** * 设置导航信息 */ private voID initNavigation() { if (!mPolic y.isadminActive()) { // Activates device administrator. setupNavigation(R.string.setup_message_activate, R.string.setup_action_activate, mActivatebuttonListener); } else if (mCurrentScreenID == R.layout.activity_policy_setup) { setupNavigation(R.string.setup_message_set_policy, R.string.setup_action_set_policy, new VIEw.OnClickListener() { public voID onClick(VIEw vIEw) { writePolicy(); mPolicy.configurePolicy(); setScreenContent(R.layout.activity_vIEw_policy); } }); } else if (!mPolicy.isActivePasswordSufficIEnt()) { // Launches password set-up screen in Settings. setupNavigation(R.string.setup_message_enforce_policy, R.string.setup_action_enforce_policy, mEnforcePolicyListener); } else { // Grants access to secure content. setupNavigation(R.string.setup_message_go_secured, R.string.setup_action_go_secured, new VIEw.OnClickListener() { public voID onClick(VIEw vIEw) { startActivity(new Intent(vIEw.getContext(), SecureActivity.class)); } }); } } /** * 监听器实现,这个不多讲了. */ private VIEw.OnClickListener mActivatebuttonListener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // First, persist the policy. Then, launch intent to trigger the system screen // requesting user to confirm the activation of the device administrator. writePolicy(); Intent activateDeviceadminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_admin); activateDeviceadminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_admin, mPolicy.getPolicyadmin()); // It is good practice to include the optional explanation text to explain to // user why the application is requesting to be a device administrator. The system // will display this message on the activation screen. activateDeviceadminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getResources() .getString(R.string.device_admin_activation_message)); startActivityForResult(activateDeviceadminIntent, REQ_ACTIVATE_DEVICE_admin); } }; /** * 监听器实现,这个小马也不多讲了. */ private VIEw.OnClickListener mEnforcePolicyListener = new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { writePolicy(); // The device administration API does not "fix" the password if it is // determined that the current password does not conform to what is requested // by the policy. The caller is responsible for triggering the password set up // screen via the below intent. Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); startActivity(intent); } }; /** * 设置激活按钮不同状态时的文本信息 * @param messageResID * @param buttonTextResID * @param Listener */ private voID setupNavigation(int messageResID, int buttonTextResID, VIEw.OnClickListener Listener) { TextVIEw setupMessage = (TextVIEw) findVIEwByID(R.ID.setup_message); setupMessage.setText(messageResID); button actionBtn = (button) findVIEwByID(R.ID.setup_action_btn); actionBtn.setText(buttonTextResID); actionBtn.setonClickListener(Listener); } // 在关闭此页时,将配置信息写入本地SP文件中. private voID writePolicy() { int passwordQuality = (int) mPasswordQualityinputFIEld.getSelectedItemID(); int passwordLength = 0; try { passwordLength = Integer.valueOf(mPasswordLengthinputFIEld.getText().toString()); } catch (NumberFormatException nfe) {} // Defaults to 0. int passwordMinUppercase = 0; try { passwordMinUppercase = Integer.valueOf(mPasswordMinUppercaseinputFIEld.getText().toString()); } catch (NumberFormatException nfe) {} // Defaults to 0. mPolicy.savetoLocal(passwordQuality, passwordLength, passwordMinUppercase); } }
配置 *** 作类:
package com.xiaoma.policymanager.demo; import androID.app.Activity; import androID.app.admin.DeviceadminReceiver; import androID.app.admin.DevicePolicyManager; import androID.content.Componentname; import androID.content.Context; import androID.content.Intent; import androID.content.SharedPreferences; import androID.os.Build; /** * @Title: Policy.java * @Package com.xiaoma.policymanager.demo * @Description: 用来获取相关安全配置信息的类 * @author MZH */ public class Policy { public static final int REQUEST_ADD_DEVICE_admin = 1; /**保存屏幕锁相关参数的SP文件名*/ private static final String APP_PREF = "APP_PREF"; /**下面三个是往SP中存储时的Key*/ private static final String KEY_PASSWORD_LENGTH = "PW_LENGTH"; private static final String KEY_PASSWORD_QUAliTY = "PW_QUAliTY"; private static final String KEY_PASSWORD_MIN_UPPERCASE = "PW_MIN_UPPERCASE"; /** * 下面是可允许输入密码的类型,此处的类型必须与string.xml文件中定义的arrays.xml中的相匹配 */ final static int[] PASSWORD_QUAliTY_VALUES = new int[] { DevicePolicyManager.PASSWORD_QUAliTY_UnspecIFIED, DevicePolicyManager.PASSWORD_QUAliTY_SOMETHING, DevicePolicyManager.PASSWORD_QUAliTY_NUMERIC, DevicePolicyManager.PASSWORD_QUAliTY_AlphaBETIC, DevicePolicyManager.PASSWORD_QUAliTY_AlphaNUMERIC, DevicePolicyManager.PASSWORD_QUAliTY_COMPLEX }; private int mPasswordQuality; private int mPasswordLength; private int mPasswordMinUpperCase; private Context mContext; private DevicePolicyManager mDPM; private Componentname mPolicyadmin; /** * 构造器,在new此类对象时,获取系统级管理员对象 DevicePolicyManager * @param context */ public Policy(Context context) { mContext = context; mPasswordQuality = -1; mPasswordLength = 0; mPasswordMinUpperCase = 0; mDPM = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POliCY_SERVICE); mPolicyadmin = new Componentname(context, Policyadmin.class); } /** * 保存设备参数 */ public voID savetoLocal(int passwordQuality, int passwordLength, int passwordMinUppercase) { SharedPreferences.Editor editor = mContext.getSharedPreferences(APP_PREF, Context.MODE_PRIVATE).edit(); if (mPasswordQuality != passwordQuality) { editor.putInt(KEY_PASSWORD_QUAliTY, passwordQuality); mPasswordQuality = passwordQuality; } if (mPasswordLength != passwordLength) { editor.putInt(KEY_PASSWORD_LENGTH, passwordLength); mPasswordLength = passwordLength; } if (mPasswordMinUpperCase != passwordMinUppercase) { editor.putInt(KEY_PASSWORD_MIN_UPPERCASE, passwordMinUppercase); mPasswordMinUpperCase = passwordMinUppercase; } editor.commit(); } /** * 从本地SP文件中获取密码配置参数 */ public voID readFromLocal() { SharedPreferences prefs = mContext.getSharedPreferences(APP_PREF, Context.MODE_PRIVATE); mPasswordQuality = prefs.getInt(KEY_PASSWORD_QUAliTY, -1); mPasswordLength = prefs.getInt(KEY_PASSWORD_LENGTH, -1); mPasswordMinUpperCase = prefs.getInt(KEY_PASSWORD_MIN_UPPERCASE, -1); } /** * 获取密码. * * @return */ public int getpasswordQuality() { return mPasswordQuality; } /** * 获取密码长度. * * @return */ public int getpasswordLength() { return mPasswordLength; } /** * 获取字母密码. * * @return */ public int getpasswordMinUpperCase() { return mPasswordMinUpperCase; } /** * 获取设备管理的 Componentname 对象. * * @return */ public Componentname getPolicyadmin() { return mPolicyadmin; } /** * 判断设备是否被激活. * * @return */ public boolean isadminActive() { return mDPM.isadminActive(mPolicyadmin); } public boolean isActivePasswordSufficIEnt() { return mDPM.isActivePasswordSufficIEnt(); } /** * 判断设备是否加安全 * @return */ public boolean isDeviceSecured() { return isadminActive() && isActiveP asswordSufficIEnt(); } /** * 在声明的mDPM对象中进行配置. */ public voID configurePolicy() { mDPM.setPasswordQuality(mPolicyadmin, PASSWORD_QUAliTY_VALUES[mPasswordQuality]); mDPM.setPasswordMinimumLength(mPolicyadmin, mPasswordLength); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mDPM.setPasswordMinimumUpperCase(mPolicyadmin, mPasswordMinUpperCase); } } /** * 下面这个类用来监听各种不同类型的设备管理事件,比如:改变密码错误密码等等 */ public static class Policyadmin extends DeviceadminReceiver { @OverrIDe public voID onDisabled(Context context, Intent intent) { // Called when the app is about to be deactivated as a device administrator. super.onDisabled(context, intent); SharedPreferences prefs = context.getSharedPreferences(APP_PREF, Activity.MODE_PRIVATE); /**清除以前在SP文件下保存的所有值*/ prefs.edit().clear().commit(); } } }
权限验证类:
package com.xiaoma.policymanager.demo;import androID.app.Activity;import androID.content.Intent;/*** @Title: SecureActivity.java* @Package com.xiaoma.policymanager.demo* @Description: 权限验证类实现* @author MZH*/public class SecureActivity extends Activity {@OverrIDeprotected voID onResume() {super.onResume();// Check to see if the device is properly secured as per the policy. Send user// back to policy set up screen if necessary.Policy policy = new Policy(this);policy.readFromLocal();if (!policy.isDeviceSecured()) {Intent intent = new Intent();intent.setClass(this, PolicySetupActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_top |Intent.FLAG_ACTIVITY_CLEAR_TASK);startActivity(intent);finish();}setContentVIEw(R.layout.activity_secure);}}
希望本文所述对你有所帮助,AndroID实现屏幕锁定源码详解内容就给大家介绍到这里了。希望大家继续关注我们的网站!想要学习AndroID可以继续关注本站。
总结以上是内存溢出为你收集整理的Android实现屏幕锁定源码详解全部内容,希望文章能够帮你解决Android实现屏幕锁定源码详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)