获得设备管理权限实现锁屏锁屏
2018-04-11 10:37
218 查看
想做个手机卫士的功能,用到锁屏,所有上网 查了下,google的官方直接给出了详细的文档。
<receiver
android:name="com.example.lockscreen.MyAdmin"
android:label="@string/app_name"
android:description="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action
android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
2.设备管理类
<span style="white-space:pre"> public class MyAdmin extends DeviceAdminReceiver {
<span style="white-space:pre"> </span>
}</span>
就是这样,可以什么都不写
3.功能代码
如果你觉得按电源键锁屏麻烦或对电源键有伤害,你点击图标就可以直接点击图标锁屏,是不是很方便。
1.文件清单
<span style="white-space:pre"> </span><!-- 设备管理 --><receiver
android:name="com.example.lockscreen.MyAdmin"
android:label="@string/app_name"
android:description="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action
android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
2.设备管理类
<span style="white-space:pre"> public class MyAdmin extends DeviceAdminReceiver {<span style="white-space:pre"> </span>
}</span>
就是这样,可以什么都不写
3.功能代码
<span style="font-size:14px;">public class LockScreen extends Activity { //设备策略服务 private DevicePolicyManager dpm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lock_screen); dpm=(DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); } public void lockscreen(View view){ ComponentName componentName= new ComponentName(this,MyAdmin.class); if (dpm.isAdminActive(componentName)) { dpm.lockNow(); dpm.resetPassword("", 0);//空为 无密码 //清除SD卡数据 // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); //恢复出厂 //dpm.wipeData(0); }else { Toast.makeText(this, "请先点击激活安全权限按钮", 1).show(); } } //用代码开启管理员权限 public void openAdmin(View view){ // 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //我yao激活谁 ComponentName componentName= new ComponentName(this,MyAdmin.class); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); //描述(additional explanation) intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "您必须开启此权限才可以使用一键锁屏"); startActivity(intent); } //卸载 public void uninstall(View view){ ComponentName componentName= new ComponentName(this,MyAdmin.class); dpm.removeActiveAdmin(componentName); //卸载应用 // Intent intent = new Intent(); // intent.setAction(""); Uri packageUri = Uri.parse("package:"+LockScreen.this.getPackageName()); Intent intent = new Intent(Intent.ACTION_DELETE,packageUri); startActivity(intent); } }</span>
4.布局文件
<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="com.example.lockscreen.MainActivity" > <Button android:onClick="lockscreen" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一键锁屏" /> <Button android:onClick="openAdmin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开启安全管理员的权限" /> <Button android:onClick="uninstall" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一键卸载" /> </RelativeLayout>
如果你觉得按电源键锁屏麻烦或对电源键有伤害,你点击图标就可以直接点击图标锁屏,是不是很方便。
相关文章推荐
- 简单几步实现Android 一键锁屏,一键卸载程序,一键激活设备管理权限
- 基于EasyNVR二次开发实现业务需求:用户、权限、设备管理
- [android] 手机卫士设备管理权限锁屏
- 浅析Android 手机卫士设备管理权限锁屏
- 基于EasyNVR二次开发实现业务需求:用户、权限、设备管理
- 基于EasyNVR二次开发实现业务需求:用户、权限、设备管理
- [android] 手机卫士设备管理权限锁屏
- 浅析Android 手机卫士设备管理权限锁屏
- 基于Spring Security2与 Ext 的权限管理设计与实现
- Winform开发框架之权限管理系统改进的经验总结(4)-一行代码实现表操作日志记录
- NHibernate3.3.0GA+ExtJS4.1.1+ASP.NET MVC3.0权限管理系统(2)--ExtJS MVC实现系统框架页
- 接下来会普及流行能实现《集中授权集中管理,分级授权逐级管理》的标准化权限系统
- Vue + Element UI 实现权限管理系统 前端篇(十四):菜单功能实现
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)
- zookeeper集群之java程序同步方式实现对数据节点的增,删,改,查及其权限管理(二)
- 应用程序不被删除,获得admin权限,锁屏
- Win7 一键获得管理所有权限(最高权限)注册表
- django 基于proxy实现用户权限管理
- springboot+springSecurity+mybatis实现权限管理
- OA权限管理的实现(下)