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

2024-03-09 10:36

本文主要是介绍android实现一键锁屏和一键卸载的方法实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

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

一.设备管理器操作步骤

1.创建类DeviceAdminReceiver的子类

如:com.itheima62.lockscreen.DeviceAdminSample

2.在清单文件中配置广播接收者

<receiverandroid:name="com.itheima62.lockscreen.DeviceAdminSample"android:description="@string/sample_device_admin_description"android:label="@string/sample_device_admin"android:permission="android.permission.BIND_DEVICE_ADMIN"  <meta-dataandroid: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 

3.配置字符串相关信息

 <string name="activity_sample_device_admin" 设备管理员</string 
<string name="sample_device_admin" 管理员</string 
<string name="sample_device_admin_description" 开启设备管理员,不开启扣2000</string 

4.在res目录下创建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 

5.在代码中创建设备管理器和组件

dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);ComponentName who = new ComponentName(this, DeviceAdminSample.class);

6.写功能

dpm.lockNow();一键锁屏

二.源代码

创建类DeviceAdminReceiver的子类

package com.example.suoping;
import android.app.admin.DeviceAdminReceiver;
public class DeviceAdminSample extends DeviceAdminReceiver
{}

MainActivity

package com.example.suoping;import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;public class MainActivity extends Activity
{private DevicePolicyManager dpm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//setContentView(R.layout.activity_main);dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);lockScreen(null);}/*** @param v* 一键锁屏*/public void lockScreen(View v){ //如果没有激活设备管理员,提醒给用户做事ComponentName who = new ComponentName(this, DeviceAdminSample.class);if (dpm.isAdminActive(who)){dpm.lockNow();//一键锁屏finish();}else {Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who);intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"设备管理器,,,,,,,,,,,,,,,,");startActivityForResult(intent, 1);}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}
}

RemoveActivity

package com.example.suoping;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;public class RemoveActivity extends Activity
{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);remove(null);}/*** 一键卸载* @param v*/public void remove(View v){ // 取消激活设备管理DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);ComponentName who = new ComponentName(this, DeviceAdminSample.class);dpm.removeActiveAdmin(who);//取消激活管理设备//卸载Intent remove = new Intent("android.intent.action.DELETE");remove.addCategory("android.intent.category.DEFAULT");remove.setData(Uri.parse("package:" + getPackageName()));startActivity(remove);//卸载用户apk的界面}
}

布局文件

MainActivity.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"android:background="#00000000" 
</RelativeLayout 

RemoveActivity.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"android:background="#00000000"</RelativeLayout 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"? 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.suoping"android:versionCode="1"android:versionName="1.0"  <uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" / <applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:theme="@style/AppTheme"  <activityandroid:name="com.example.suoping.MainActivity"android:label="一键锁屏"  <intent-filter <action android:name="android.intent.action.MAIN" / <category android:name="android.intent.category.LAUNCHER" / </intent-filter </activity <activityandroid:name="com.example.suoping.RemoveActivity"android:label="一键卸载"  <intent-filter <action android:name="android.intent.action.MAIN" / <category android:name="android.intent.category.LAUNCHER" / </intent-filter </activity <receiverandroid:name="com.example.suoping.DeviceAdminSample"android:description="@string/sample_device_admin_description"android:label="@string/sample_device_admin"android:permission="android.permission.BIND_DEVICE_ADMIN"  <meta-dataandroid: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 

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 

strings.xml

<resources <string name="app_name" 一键锁屏</string <string name="action_settings" Settings</string <string name="hello_world" Hello world!</string <string name="activity_sample_device_admin" 设备管理员</string <string name="sample_device_admin" 管理员</string <string name="sample_device_admin_description" 开启设备管理员,不开启扣2000</string 
</resources 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ZaLou.Cn的支持。

更多Android进阶指南 可以扫码 解锁 《Android十大板块文档》

1.Android车载应用开发系统学习指南(附项目实战)

2.Android Framework学习指南,助力成为系统级开发高手

3.2023最新Android中高级面试题汇总+解析,告别零offer

4.企业级Android音视频开发学习路线+项目实战(附源码)

5.Android Jetpack从入门到精通,构建高质量UI界面

6.Flutter技术解析与实战,跨平台首要之选

7.Kotlin从入门到实战,全方面提升架构基础

8.高级Android插件化与组件化(含实战教程和源码)

9.Android 性能优化实战+360°全方面性能调优

10.Android零基础入门到精通,高手进阶之路

敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔

这篇关于android实现一键锁屏和一键卸载的方法实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/790413

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Linux系统中查询JDK安装目录的几种常用方法

《Linux系统中查询JDK安装目录的几种常用方法》:本文主要介绍Linux系统中查询JDK安装目录的几种常用方法,方法分别是通过update-alternatives、Java命令、环境变量及目... 目录方法 1:通过update-alternatives查询(推荐)方法 2:检查所有已安装的 JDK方