android实现一键锁屏和一键卸载的方法实例

  

  

本文主要介绍了关于android实现一键锁屏和一键卸载的相关内容,分享出来供大家参考学习,这两个功能也是大家在开发中会遇到的两个需求,下面话不多说了,来一起看看详细的介绍吧。

  

  

<强> 1。创建类DeviceAdminReceiver的子类

  

如:<代码> com.itheima62.lockscreen.DeviceAdminSample

  

<强> 2。在清单文件中配置广播接收者

        & lt;接收机   android: name=" com.itheima62.lockscreen.DeviceAdminSample "   android:描述=" @string/sample_device_admin_description”   android: label=" @string/sample_device_admin”   android:许可=" android.permission。祝辞BIND_DEVICE_ADMIN”;   & lt;元数据   android: name=" android.app.device_admin "   android:资源=癅xml/device_admin_sample”/比;      & lt; intent-filter>   & lt;行动android: name=" android.app.action。DEVICE_ADMIN_ENABLED”/比;   & lt;/intent-filter>   & lt;/receiver>      

<强> 3。配置字符串相关信息

        & lt;字符串名称=" activity_sample_device_admin "祝辞设备管理员& lt;/string>   & lt;字符串名称=" sample_device_admin "祝辞管理员& lt;/string>   & lt;字符串名称=皊ample_device_admin_description”在开启设备管理员,不开启扣2000块& lt;/string>      

<>强4。在res目录下创建xml文件夹,在该文件夹下创建device_admin_sample.xml文件,内容:

        & lt; device-admin xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt; uses-policies>   & lt; limit-password/比;   & lt; watch-login/比;   & lt; reset-password/比;   & lt; force-lock/比;   & lt;删除数据/比;   & lt; expire-password/比;   & lt;加密存储/比;   & lt; disable-camera/比;   & lt;/uses-policies>   & lt;/device-admin>      

<强> 5。在代码中创建设备管理器和组件

        dpm=(DevicePolicyManager) getSystemService (DEVICE_POLICY_SERVICE);   ComponentName=新ComponentName(这个,DeviceAdminSample.class);      

<强> 6。写功能

        dpm.lockNow();一键锁屏      

  

创建类DeviceAdminReceiver的子类

        包com.example.suoping;   进口android.app.admin.DeviceAdminReceiver;   公共类DeviceAdminSample DeviceAdminReceiver延伸   {      }      

MainActivity         包com.example.suoping;      进口android.app.Activity;   进口android.app.admin.DevicePolicyManager;   进口android.content.ComponentName;   进口android.content.Intent;   进口android.os.Bundle;   进口android.view.Menu;   进口android.view.View;      公开课MainActivity延伸活动   {   私人DevicePolicyManager dpm;   @Override   保护空白>   包com.example.suoping;      进口android.app.Activity;   进口android.app.admin.DevicePolicyManager;   进口android.content.ComponentName;   进口android.content.Intent;   进口android.net.Uri;   进口android.os.Bundle;   进口android.view.View;      公开课RemoveActivity延伸活动   {   @Override   保护空白>   & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:背景=" # 00000000 "比;   & lt;/RelativeLayout>      

RemoveActivity.xml         & lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "   xmlns:工具=" http://schemas.android.com/tools "   android: layout_width=" match_parent "   android: layout_height=" match_parent "   android:背景=" # 00000000 "   比;   & lt;/RelativeLayout>      

AndroidManifest.xml         & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;清单xmlns: android=" http://schemas.android.com/apk/res/android "   包=" com.example.suoping "   android: versionCode=" 1 "   android: versionName=" 1.0 "比;   & lt; uses-sdk   android: minSdkVersion=" 8 "   android: targetSdkVersion=?8”/比;      & lt;应用程序   android: allowBackup=" true "   android:图标=" @drawable/ic_launcher”   android:主题=癅style/AppTheme”比;   & lt;活动   android: name=" com.example.suoping.MainActivity "   android: label="一键锁屏”在   & lt; intent-filter>   & lt;行动android: name=" android.intent.action.MAIN "/比;      & lt;类别android: name=" android.intent.category.LAUNCHER "/比;   & lt;/intent-filter>   & lt;/activity>   & lt;活动   android: name=" com.example.suoping.RemoveActivity "   android: label="一键卸载”比;   & lt; intent-filter>   & lt;行动android: name=" android.intent.action.MAIN "/比;      & lt;类别android: name=" android.intent.category.LAUNCHER "/比;   & lt;/intent-filter>   & lt;/activity>      & lt;接收机   android: name=" com.example.suoping.DeviceAdminSample "   android:描述=" @string/sample_device_admin_description”   android: label=" @string/sample_device_admin”   android:许可=" android.permission。祝辞BIND_DEVICE_ADMIN”;   & lt;元数据   android: name=" android.app.device_admin "   android:资源=癅xml/device_admin_sample”/比;   & lt; intent-filter>   & lt;行动android: name=" android.app.action。DEVICE_ADMIN_ENABLED”/比;   & lt;/intent-filter>   & lt;/receiver>   & lt;/application>   & lt;/manifest>

android实现一键锁屏和一键卸载的方法实例