輕松實(shí)現(xiàn)Android鎖屏功能
鎖屏需要引入設(shè)備超級管理員。在文檔Android開發(fā)文檔的Administration中有詳細(xì)的說明。Android設(shè)備管理系統(tǒng)功能和控制訪問。
主要有一下幾個(gè)步驟:
1 創(chuàng)建廣播接收者,實(shí)現(xiàn)DeviceAdminReceiver
package com.andy.lockscreen;
import android.app.admin.DeviceAdminReceiver;
/**
* @author Zhang,Tianyou
* @version 2014年11月20日 下午9:51:42
*
* 特殊的廣播接受者 接收 管理員權(quán)限廣播
*/
public class MyAdmin extends DeviceAdminReceiver{
}
2 在清單文件中注冊該廣播(不同普通的廣播,需按照說明格式):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.andy.lockscreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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=".MyAdmin"
android:description="@string/sample_device_admin_description"
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>
</application>
</manifest>
3 在res下創(chuàng)建xml文件夾,創(chuàng)建對應(yīng)的xml文件device_admin_sample.xml
<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 /> </uses-policies> </device-admin>
4 在values文件下string.xml添加
<string name="sample_device_admin_description">用戶管理員的描述信息</string> <string name="sample_device_admin">設(shè)置管理權(quán)限</string>
5 界面文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.andy.lockscreen.MainActivity" >
<Button
android:onClick="openAdmin"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟管理員權(quán)限" />
<Button
android:onClick="lockcreen"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="一鍵鎖屏" />
<Button
android:onClick="uninstall"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卸載鎖屏" />
</RelativeLayout>
6 實(shí)現(xiàn)鎖屏和開啟設(shè)備管理員權(quán)限,卸載文件
package com.andy.lockscreen;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/**
* 設(shè)備策略服務(wù)
*/
private DevicePolicyManager dpm;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}
/**
* 鎖屏
*
* @param view
*/
public void lockcreen(View view) {
ComponentName who = new ComponentName(this, MyAdmin.class);
// 判斷是否已經(jīng)開啟管理員權(quán)限
if (dpm.isAdminActive(who)) {
// 鎖屏
dpm.lockNow();
// 設(shè)置屏幕密碼 第一個(gè)是密碼 第二個(gè)是附加參數(shù)
dpm.resetPassword("123", 0);
// 清楚數(shù)據(jù)
// WIPE_EXTERNAL_STORAGE 清楚sdcard的數(shù)據(jù)
// 0 恢復(fù)出廠設(shè)置
//dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
} else {
// 如果為未開啟 提示
Toast.makeText(MainActivity.this, "請先開啟管理員權(quán)限!", Toast.LENGTH_SHORT)
.show();
}
}
/**
* 代碼開啟管理權(quán)限
*
* @param view
*/
public void openAdmin(View view) {
// 創(chuàng)建一個(gè)Intent 添加設(shè)備管理員
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// 激活MyAdmin廣播接收著
ComponentName who = new ComponentName(this, MyAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);
// 說明用戶開啟管理員權(quán)限的好處
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"開啟可以一鍵鎖屏,防止勿碰");
startActivity(intent);
Toast.makeText(MainActivity.this, "管理員權(quán)限已開啟!", Toast.LENGTH_SHORT).show();
}
/**
* 卸載當(dāng)前的軟件 設(shè)備管理數(shù)據(jù)特殊應(yīng)用 所以不能普通卸載
*/
public void uninstall(View view) {
// 1. 先清除管理員權(quán)限
ComponentName who = new ComponentName(this,
MyAdmin.class);
dpm.removeActiveAdmin(who);
// 2. 普通應(yīng)用的卸載
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:"+getPackageName()));
startActivity(intent);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容,主要通過設(shè)置EditText的setTransformationMethod()方法來實(shí)現(xiàn),需要的朋友可以參考下2014-09-09
Android開發(fā)使用Databinding實(shí)現(xiàn)關(guān)注功能mvvp
這篇文章主要為大家介紹了Android開發(fā)使用Databinding實(shí)現(xiàn)關(guān)注功能mvvp示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件
這篇文章主要介紹了Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件的相關(guān)資料,需要的朋友可以參考下2017-03-03
ViewFlipper實(shí)現(xiàn)文字輪播效果
這篇文章主要為大家詳細(xì)介紹了ViewFlipper實(shí)現(xiàn)文字輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Android開發(fā)導(dǎo)入項(xiàng)目報(bào)錯(cuò)Ignoring InnerClasses attribute for an anonym
今天小編就為大家分享一篇關(guān)于Android開發(fā)導(dǎo)入項(xiàng)目報(bào)錯(cuò)Ignoring InnerClasses attribute for an anonymous inner class的解決辦法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開(親測可用)
跳轉(zhuǎn)手機(jī)熱點(diǎn)的頁面肯定是用intent,重點(diǎn)是action不知道是什么,網(wǎng)上最常見的就是Settings.ACTION_WIFI_SETTINGS 跳轉(zhuǎn)wifi設(shè)置頁面,本文介紹Android?kotlin?跳轉(zhuǎn)手機(jī)熱點(diǎn)開關(guān)頁面和判斷熱點(diǎn)是否打開,感興趣的朋友一起看看吧2023-08-08
簡單實(shí)現(xiàn)Android滾動(dòng)公告欄
這篇文章主要為大家詳細(xì)介紹了如何簡單實(shí)現(xiàn)Android滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android對圖片Drawable實(shí)現(xiàn)變色示例代碼
這篇文章主要給大家介紹了關(guān)于Android對圖片Drawable實(shí)現(xiàn)變色的相關(guān)資料,文中通過示例代碼將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-08-08
Android 動(dòng)畫之ScaleAnimation應(yīng)用詳解
本節(jié)講解ScaleAnimation 動(dòng)畫在應(yīng)用中的實(shí)現(xiàn),有需要的朋友可以參考下2012-12-12

