经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android编程程序实现一键锁屏的方法讲解
来源:jb51  时间:2019/3/28 8:54:10  对本文有异议

Android程序之一键锁屏

(1)布局文件activity_main.xml如下:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity" >
  6. <Button
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:onClick="openAdmin"
  10. android:text="开启管理员权限" />
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_centerInParent="true"
  15. android:onClick="lockscreen"
  16. android:text="一键锁屏" />
  17. <Button
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentBottom="true"
  21. android:onClick="uninstall"
  22. android:text="卸载软件" />
  23. </RelativeLayout>

(2)MainActivity.java

  1. package com.xuliugen.lockscreen;
  2. import com.itheima.lockscreen.R;
  3. import android.app.Activity;
  4. import android.app.admin.DevicePolicyManager;
  5. import android.content.ComponentName;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12. /**
  13. * 设备策略服务
  14. */
  15. private DevicePolicyManager dpm;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
  21. }
  22. /**
  23. * 用代码去开启管理员
  24. */
  25. public void openAdmin(View view) {
  26. // 创建一个Intent
  27. Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  28. // 我要激活谁
  29. ComponentName mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
  30. intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mDeviceAdminSample);
  31. // 劝说用户开启管理员权限
  32. intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"哥们开启我可以一键锁屏,你的按钮就不会经常失灵");
  33. startActivity(intent);
  34. }
  35. /**
  36. * 一键锁屏
  37. */
  38. public void lockscreen(View view) {
  39. ComponentName who = new ComponentName(this, MyAdmin.class);
  40. if (dpm.isAdminActive(who)) {
  41. dpm.lockNow();// 锁屏
  42. dpm.resetPassword("", 0);// 设置屏蔽密码
  43. // 清除Sdcard上的数据
  44. // dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
  45. // 恢复出厂设置
  46. // dpm.wipeData(0);
  47. } else {
  48. Toast.makeText(this, "还没有打开管理员权限", 1).show();
  49. return;
  50. }
  51. }
  52. /**
  53. * 卸载当前软件
  54. */
  55. public void uninstall(View view) {
  56. // 1.先清除管理员权限
  57. ComponentName mDeviceAdminSample = new ComponentName(this,
  58. MyAdmin.class);
  59. dpm.removeActiveAdmin(mDeviceAdminSample);
  60. // 2.普通应用的卸载
  61. Intent intent = new Intent();
  62. intent.setAction("android.intent.action.VIEW");
  63. intent.addCategory("android.intent.category.DEFAULT");
  64. intent.setData(Uri.parse("package:" + getPackageName()));
  65. startActivity(intent);
  66. }
  67. }

(3)根据API文档可知,需要一个类继承DeviceAdminReceiver:

  1. package com.xuliugen.lockscreen;
  2. import android.app.admin.DeviceAdminReceiver;
  3. /**
  4. * 特殊的广播接收者
  5. * @author xuliugen
  6. */
  7. public class MyAdmin extends DeviceAdminReceiver {
  8. }

(4)广播接受者的设置(清单文件):

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.xuliugen.lockscreen"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="10"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.xuliugen.lockscreen.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <!-- 广播接收者 -->
  23. <receiver
  24. android:name="com.xuliugen.lockscreen.MyAdmin"
  25. android:description="@string/sample_device_admin_description"
  26. android:label="@string/sample_device_admin"
  27. android:permission="android.permission.BIND_DEVICE_ADMIN" >
  28. <meta-data
  29. android:name="android.app.device_admin"
  30. android:resource="@xml/device_admin_sample" />
  31. <intent-filter>
  32. <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
  33. </intent-filter>
  34. </receiver>
  35. </application>
  36. </manifest>

运行效果:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号