本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~
Android从API2.2开始供提了备设管理的API。可以实现以下功能:
一.密码管理
密码管理前目只用于屏幕解锁
密码管理供提的略策:
2.2API
1.小最密码长度
2.包括字母字数密码
3.最少包括字母
3.0 API
1.杂复密码(最少一个字数,一个字母,一个特殊符号 )
2.起码字母
3.起码小写
4.起码非字母字符
5.起码字数
6.起码符号
7.起码大写
8.密码过期时光
9.密码历史制限(止防用户重用去过的密码)
10.大最试尝次数
二.据数管理
2.2API:擦除据数
三.硬件管理
2.2API:锁屏
锁屏略策:
1. 当即锁屏
2. 大最间隔时光锁屏
4.0API:止禁用使摄像头
4.2API:止禁用使软键盘
注:一个手机可以安装多个备设管理序程,如果同时存在多个备设管理序程并且都处于启用态状的话,有所管理序程中最严厉的略策被执行。
实现备设管理的步调:
一.在码代中,写一个类实现DeviceAdminReceiver接口
public static class DeviceAdminTestReceiver extends DeviceAdminReceiver { @Override public void onEnabled(Context context, Intent intent) { showToast(context, "启用了略策"); } @Override public CharSequence onDisableRequested(Context context, Intent intent) { showToast(context, "试图停用略策"); return null; } @Override public void onDisabled(Context context, Intent intent) { showToast(context, "停用了略策"); } @Override public void onPasswordChanged(Context context, Intent intent) { showToast(context, "改变了密码"); } @Override public void onPasswordFailed(Context context, Intent intent) { showToast(context, "密码试尝失败"); } @Override public void onPasswordSucceeded(Context context, Intent intent) { showToast(context, "密码试尝胜利"); } @Override public void onPasswordExpiring(Context context, Intent intent) { showToast(context, "密码过期"); } void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }
每一个调回方法中可以对事件做出应相的处置,这里只是简略做Toast处置
二.在单清件文中如下配置
<receiver android:name=".app.DeviceAdminSample$DeviceAdminTestReceiver" android:label="@string/sample_device_admin" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_sample" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver>
android:name=".app.DeviceAdminSample$DeviceAdminTestReceiver"指出刚才在码代中新建的类
<meta-data>中,android:name="android.app.device_admin"是牢固写法,
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />也是牢固写法
android:resource="@xml/device_admin_sample"指向一个xml件文,件文中配置了备设管理须要用到的略策
device_admin_sample.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 /> <expire-password /> <encrypted-storage /> <disable-camera /> <disable-keyguard-features /> </uses-policies> </device-admin>
三.在序程中启动备设管理。
DevicePolicyManager mDPM; ComponentName mComponentName; mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); mComponentName = new ComponentName(this, DeviceAdminTestReceiver.class); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName); startActivityForResult(intent, 1);
用调此段码代后,用户面界会弹出一个页面提示否是启动备设管理以及备设管理所用使的略策,列出来的略策是在刚才xml件文中配置的
用户选择了激活后之启用备设管理。
如果要止禁一个已启用的略策,用调如下码代
DevicePolicyManager mDPM; ...... removeActiveAdmin(ComponentName who);
ComponentName是序程中DeviceAdminReceiver实例的ComponentName对象,可以通过如下方法到得
new ComponentName(this,DeviceAdminTestReceiver.class);
四.通过码代设置应相的略策和管理
(一).密码略策
在分析密码略策前先分析一下解锁:
在一般的手机上,按源电键后会关闭屏幕,再次按源电键时会点亮屏幕,一般的手机遇示显一个滑动条,提示滑动解锁,动拖滑动条回到关闭屏幕前的页面。
滑动解锁只是有所屏幕解锁式方的一种。Android系统供提了以下5种解锁式方:
1.无 :没有解锁,点亮屏幕时直接返回关闭屏幕前的页面
2.滑动:通过动拖解锁钮按解锁,如上图
3.图案:通过点与点之间的连接发生图案解锁,如下图:
4.PIN:纯字数密码解锁
5.密码:混合密码解锁
通过API设置的密码略策会对PIN和密码这2种解锁式方起作用。
面下列出设置密码略策的API,这些方法都来自于DevicePolicyManager。每次设置解锁密码时,系统会查检设置的密码否是符合略策,如果符合,才能设置胜利。如果后以已设置了密码,则新设定的略策在下次设置密码时失效。
例如,通过
DevicePolicyManager mDPM; ComponentName mDeviceAdminSample; ... setPasswordMinimumLength(ComponentName, 4)
设置了小最长度为4,下次变动密码时,如果输入的新密码不符合长度,则面界示显:
setPasswordExpirationTimeout(ComponentName admin, long timeout) |
密码过期时光,第二个参数是时光,位单为毫秒 |
setPasswordHistoryLength(ComponentName admin, int length) |
密码历史长度,第二个参数是记载历史密码的次数,如果设置为2,则表现新密码和前2次设置的不能雷同 |
setPasswordMinimumLength(ComponentName admin, int length) |
密码小最长度 |
setPasswordMinimumLetters(ComponentName admin, int length) |
密码起码须要的字母 |
setPasswordMinimumLowerCase(ComponentName admin, int length) |
密码起码须要的小写字母 |
setPasswordMinimumNonLetter(ComponentName admin, int length) |
对于字数和字符构成的密码,起码须要的字数 |
setPasswordMinimumNumeric(ComponentName admin, int length) |
密码起码须要的字数 |
setPasswordMinimumSymbols(ComponentName admin, int length) |
密码起码须要的符号 |
setPasswordMinimumUpperCase(ComponentName admin, int length) |
密码起码须要的大写字母 |
setPasswordQuality(ComponentName admin, int quality) |
设置密码品质,是比拟糙粗的略策,在3.0以下的API中用使,第二个参数是对应略策的常量 |
setPasswordExpirationTimeout(ComponentName admin, long timeout) |
密码过期时光,位单毫秒 |
setMaximumFailedPasswordsForWipe(ComponentName admin, int num) |
试尝密码失败多少次后擦除据数 第二个参数为次数,不议建用使此方法,可以在实现DeviceAdminReceiver接口的类的 onPasswordFailed调回方法里头灵活处置 |
(二)设置密码
设置密码有2种式方
1.启动用户设置面界设置(通过系统指定的Intent启动设置Activity)
2.通过API设置
启动用户设置面界设置:
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); startActivity(intent);
用调此段码代后用户面界示显如下,点击后2个选项可以人工设置屏幕解锁密码
点击最后一个选项“密码”后,跳出如面下界:
续连输入2次密码并且符合密码略策后设置胜利
另外一种设置密码的式方是用DevicePolicyManager的API:
resetPassword (String newPsw, int flags)
用调此API时传入的新密码必须符合后以的密码略策,不然法无起作用。
第二个参数如果设置为DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY,则通过用户面界进入密码设置前须要输入通过此API重设的密码。
(三)锁屏
锁屏2种式方:
1.立刻锁屏
2.设置定一时光内无作操锁屏
立刻锁屏:
DevicePolicyManager mDPM; ……… mDPM.lockNow();
设置定一时光内无作操锁屏
DevicePolicyManager mDPM; ComponentName mDeviceAdminSample; ... long timeMs = 1000L*Long.parseLong(mTimeout.getText().toString()); mDPM.setMaximumTimeToLock(mDeviceAdminSample, timeMs);
(四)摄像头管理
启用/禁用:
setCameraDisabled (ComponentName admin, boolean disabled)
参数 disabled:true停用,false启用
停用后开打相机功能,如下图:
(五)擦除据数
擦除据数2种式方
1. 擦除用应据数
2. 擦除用应据数和擦除sd卡据数
擦除用应据数
DevicePolicyManager mDPM; …… mDPM.wipeData(0);
擦除用应据数和擦除sd卡据数
DevicePolicyManager mDPM; …… mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
备设管理实例
Java码代:
package com.example.devicemanagetest; import java.util.List; import android.app.admin.DeviceAdminReceiver; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; import android.widget.Toast; public class MainActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); mComponentName = new ComponentName(this, DeviceAdminTestReceiver.class); } @Override public void onBuildHeaders(List<Header> target) { super.onBuildHeaders(target); loadHeadersFromResource(R.xml.device_admin_headers, target); } public static class BaseFragment extends PreferenceFragment { MainActivity mMainActivity; DevicePolicyManager mDPM; ComponentName mComponentName; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mMainActivity = (MainActivity)getActivity(); mDPM = mMainActivity.mDPM; mComponentName = mMainActivity.mComponentName; } } /*** * 启动/停用 */ public static class EnableFragment extends BaseFragment implements OnPreferenceChangeListener{ CheckBoxPreference mEnable; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.device_admin_enable); mEnable = (CheckBoxPreference) findPreference("key_enable"); mEnable.setOnPreferenceChangeListener(this); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { boolean ifEnable = (Boolean)newValue; if (ifEnable != mDPM.isAdminActive(mComponentName)) { if (ifEnable) { enableAdmin(); } else { mDPM.removeActiveAdmin(mComponentName); } } return true; } private void enableAdmin() { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName); startActivityForResult(intent, 1); } } /** * 密码略策 * */ public static class QualityFragment extends BaseFragment implements OnPreferenceChangeListener{ CheckBoxPreference alphabeticPreference; CheckBoxPreference alphanumericPreference; EditTextPreference minLetter; EditTextPreference minTimeOut; EditTextPreference maxWipe; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.device_admin_pswquality); alphabeticPreference = (CheckBoxPreference) findPreference("key_alphabetic"); alphanumericPreference = (CheckBoxPreference) findPreference("key_alphanumeric"); alphabeticPreference.setOnPreferenceChangeListener(this); alphanumericPreference.setOnPreferenceChangeListener(this); } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference == alphabeticPreference) { boolean value = (Boolean)newValue; if (value) { //字母 mDPM.setPasswordQuality(mComponentName, DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC); } } else if (preference == alphanumericPreference) { boolean value = (Boolean)newValue; if (value) { //字母字数 mDPM.setPasswordQuality(mComponentName, DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC); } } else if (preference == minLetter) { int minLetter = (Integer)newValue; mDPM.setPasswordMinimumLetters(mComponentName, minLetter); } else if (preference == minTimeOut) { int day = (Integer)newValue; long minTimeOut = 86400 * 1000 * day; mDPM.setPasswordExpirationTimeout(mComponentName, minTimeOut); } else if (preference == maxWipe) { int time = (Integer)newValue; mDPM.setMaximumFailedPasswordsForWipe(mComponentName, time); } return true; } } /** * 设置密码 * */ public static class PswSettingFragment extends BaseFragment implements OnPreferenceClickListener, OnPreferenceChangeListener{ PreferenceScreen change; EditTextPreference reset; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.device_admin_setpsw); change = (PreferenceScreen) findPreference("key_set_password"); reset = (EditTextPreference) findPreference("key_reset_password"); change.setOnPreferenceClickListener(this); reset.setOnPreferenceChangeListener(this); } @Override public boolean onPreferenceClick(Preference preference) { //点击了设置解密式方,如果选择的式方没有设置密码,则行进设置 if (preference == change) { Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD); startActivity(intent); } return false; } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference == reset) { //立刻重设密码,重设前须要解密 String newPassword = String.valueOf(newValue); mDPM.resetPassword(newPassword, DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY); } return true; } } /** * 屏幕锁/相机 * */ public static class LockFragment extends BaseFragment implements OnPreferenceChangeListener, OnPreferenceClickListener{ CheckBoxPreference camera; PreferenceScreen lockNow; EditTextPreference lockAtTime; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.device_admin_lock); lockNow = (PreferenceScreen) findPreference("key_lock"); lockAtTime = (EditTextPreference) findPreference("key_lock_time"); camera = (CheckBoxPreference)findPreference("key_disable_camera"); lockNow.setOnPreferenceClickListener(this); lockAtTime.setOnPreferenceChangeListener(this); camera.setOnPreferenceChangeListener(this); } @Override public boolean onPreferenceClick(Preference preference) { //立刻锁屏 mDPM.lockNow(); return false; } @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference == lockAtTime) { // 多久后以锁屏,毫秒位单 long timeMs = 1000L * Long.parseLong(String.valueOf(newValue)); mDPM.setMaximumTimeToLock(mComponentName, timeMs); } else if (preference == camera) { boolean ifEnable = (Boolean)newValue; if (ifEnable) { mDPM.setCameraDisabled(mComponentName, true); } else { mDPM.setCameraDisabled(mComponentName, false); } } return true; } } /** * 擦除据数/据数加密 * */ public static class WipeFragment extends BaseFragment implements OnPreferenceClickListener{ PreferenceScreen wipeApp; PreferenceScreen wipeSD; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.device_admin_wipe); wipeApp = (PreferenceScreen) findPreference("wipe_ram"); wipeSD = (PreferenceScreen) findPreference("wipe_sdcard"); wipeApp.setOnPreferenceClickListener(this); wipeSD.setOnPreferenceClickListener(this); } @Override public boolean onPreferenceClick(Preference preference) { if (preference == wipeApp) { mDPM.wipeData(0); } else if (preference == wipeSD) { mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); } return false; } } /** * 处置略策的receiver * */ public static class DeviceAdminTestReceiver extends DeviceAdminReceiver { @Override public void onEnabled(Context context, Intent intent) { showToast(context, "启用了略策"); } @Override public CharSequence onDisableRequested(Context context, Intent intent) { showToast(context, "试图停用略策"); return null; } @Override public void onDisabled(Context context, Intent intent) { showToast(context, "停用了略策"); } @Override public void onPasswordChanged(Context context, Intent intent) { showToast(context, "改变了密码"); } @Override public void onPasswordFailed(Context context, Intent intent) { showToast(context, "密码试尝失败"); } @Override public void onPasswordSucceeded(Context context, Intent intent) { showToast(context, "密码试尝胜利"); } @Override public void onPasswordExpiring(Context context, Intent intent) { showToast(context, "密码过期"); } void showToast(Context context, String msg) { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } } private DevicePolicyManager mDPM; private ComponentName mComponentName; }
单清件文:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.devicemanagetest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.devicemanagetest.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="com.example.devicemanagetest.MainActivity$DeviceAdminTestReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN"> <meta-data android:name="android.app.device_admin" android:resource="@xml/device_admin_list" /> <intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> </intent-filter> </receiver> </application> </manifest>
xml件文:
device_admin_list.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 /> <expire-password /> <encrypted-storage /> <disable-camera /> <disable-keyguard-features /> </uses-policies> </device-admin>
device_admin_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:fragment="com.example.devicemanagetest.MainActivity$EnableFragment" android:title="启用/停用" /> <header android:fragment="com.example.devicemanagetest.MainActivity$QualityFragment" android:title="密码略策" /> <header android:fragment="com.example.devicemanagetest.MainActivity$PswSettingFragment" android:title="密码设置" /> <header android:fragment="com.example.devicemanagetest.MainActivity$LockFragment" android:title="屏幕锁/摄像头" /> <header android:fragment="com.example.devicemanagetest.MainActivity$WipeFragment" android:title="擦除据数/加密" /> </preference-headers>
device_admin_enable.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="启用停用" > <CheckBoxPreference android:key="key_enable" android:title="启用/停用" android:summary="启用/停用略策" /> </PreferenceCategory> </PreferenceScreen>
device_admin_lock.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="锁屏设置" > <PreferenceScreen android:key="key_lock" android:title="立刻锁屏" ></PreferenceScreen> <EditTextPreference android:key="key_lock_time" android:title="锁屏时光(分钟)" /> <CheckBoxPreference android:key="key_disable_camera" android:title="止禁/启动摄像头" /> </PreferenceCategory> </PreferenceScreen>
device_admin_pswquality.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="密码强度" > <CheckBoxPreference android:key="key_alphabetic" android:title="PASSWORD_QUALITY_ALPHABETIC" android:summary="最少包括字母" /> <CheckBoxPreference android:key="key_alphanumeric" android:title="PASSWORD_QUALITY_ALPHANUMERIC" android:summary="字母字数" /> </PreferenceCategory> <PreferenceCategory android:title="更细详的设置" > <EditTextPreference android:key="key_min_letter" android:title="setPasswordMinimumLetters()" android:summary="起码须要的字母" /> <EditTextPreference android:key="key_min_timeout" android:title="setPasswordExpirationTimeout()" android:summary="密码过期时光" /> <EditTextPreference android:key="key_max_wipe" android:title="setMaximumFailedPasswordsForWipe()" android:summary="大最试尝次数后擦除" /> </PreferenceCategory> </PreferenceScreen>
device_admin_setpsw.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="密码" > <PreferenceScreen android:key="key_set_password" android:title="解密式方" /> <EditTextPreference android:key="key_reset_password" android:title="新重设置密码" android:dialogTitle="设置字文密码" /> </PreferenceCategory> </PreferenceScreen>
device_admin_wipe.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="据数擦除/加密" > <PreferenceScreen android:title="擦除用应据数" android:key="wipe_ram" ></PreferenceScreen> <PreferenceScreen android:title="擦除sd卡据数" android:key="wipe_sdcard" ></PreferenceScreen> </PreferenceCategory> </PreferenceScreen>
文章结束给大家分享下程序员的一些笑话语录: Google事件并不像国内主流媒体普遍误导的那样,它仅仅是中国Z府和美国公司、中国文化和美国文化甚至中国人和美国人之间的关系,是民族主义和帝国主义之间的关系;更重要的是,它就是Z府和公司之间的关系,是权力管制和市场自由之间的关系。从这个意义上说,过度管制下的受害者,主要是国内的企业。Google可以抽身而去,国内的企业只能祈望特区。www.ishuo.cn