Android編程程序?qū)崿F(xiàn)一鍵鎖屏的方法講解
Android程序之一鍵鎖屏
(1)布局文件activity_main.xml如下:
<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" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="openAdmin" android:text="開啟管理員權(quán)限" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:onClick="lockscreen" android:text="一鍵鎖屏" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:onClick="uninstall" android:text="卸載軟件" /> </RelativeLayout>
(2)MainActivity.java
package com.xuliugen.lockscreen; import com.itheima.lockscreen.R; 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) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); } /** * 用代碼去開啟管理員 */ public void openAdmin(View view) { // 創(chuàng)建一個(gè)Intent Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); // 我要激活誰 ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceAdminSample); // 勸說用戶開啟管理員權(quán)限 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"哥們開啟我可以一鍵鎖屏,你的按鈕就不會(huì)經(jīng)常失靈"); startActivity(intent); } /** * 一鍵鎖屏 */ public void lockscreen(View view) { ComponentName who = new ComponentName(this, MyAdmin.class); if (dpm.isAdminActive(who)) { dpm.lockNow();// 鎖屏 dpm.resetPassword("", 0);// 設(shè)置屏蔽密碼 // 清除Sdcard上的數(shù)據(jù) // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); // 恢復(fù)出廠設(shè)置 // dpm.wipeData(0); } else { Toast.makeText(this, "還沒有打開管理員權(quán)限", 1).show(); return; } } /** * 卸載當(dāng)前軟件 */ public void uninstall(View view) { // 1.先清除管理員權(quán)限 ComponentName mDeviceAdminSample = new ComponentName(this, MyAdmin.class); dpm.removeActiveAdmin(mDeviceAdminSample); // 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); } }
(3)根據(jù)API文檔可知,需要一個(gè)類繼承DeviceAdminReceiver:
package com.xuliugen.lockscreen; import android.app.admin.DeviceAdminReceiver; /** * 特殊的廣播接收者 * @author xuliugen */ public class MyAdmin extends DeviceAdminReceiver { }
(4)廣播接受者的設(shè)置(清單文件):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xuliugen.lockscreen" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" 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.xuliugen.lockscreen.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.xuliugen.lockscreen.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>
運(yùn)行效果:
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端
這篇文章主要為大家詳細(xì)介紹了Android模擬實(shí)現(xiàn)網(wǎng)易新聞客戶端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05詳解如何在Flutter中用小部件創(chuàng)建響應(yīng)式布局
由于Flutter的跨平臺、單一代碼庫的能力,了解屏幕管理以防止像柔性溢出錯(cuò)誤或糟糕的用戶界面設(shè)計(jì)這樣的問題是至關(guān)重要的。本文將探討如何用靈活和擴(kuò)展的小部件創(chuàng)建響應(yīng)式布局,需要的可以參考一下2022-02-02詳解Android .9.png “點(diǎn)九”圖片的使用
這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下2016-09-09Android Studio 3.0后出現(xiàn)AAPT2與“android.enableAapt2”問題的解決方法
這篇文章主要給大家介紹了關(guān)于Android Studio 3.0后出現(xiàn)AAPT2與“android.enableAapt2”問題的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法
這篇文章主要介紹了Android 中ListView的Item點(diǎn)擊事件失效的快速解決方法的相關(guān)資料,需要的朋友可以參考下2016-09-09