• 锁屏DevicePolicyManager申请系统管理权限


    效果图:

      

    Caused by:  java.lang.SecurityException: No active admin owned by uid 10038 for policy #3
    该错误需要把应用程序升级为拥有系统管理员权限,写一个广播接收者,给该广播接收者去申请系统管理员的权限,让操作系统给广播接收者授权。(其实就去激活系统的一个授权的组件)让用户自己激活。

    锁屏步骤:

    1.创建 MyAdmin 的广播接受者 继承 DeviceAdminReceiver

    1 package com.example.lockscreen;
    2 
    3 import android.app.admin.DeviceAdminReceiver;
    4 
    5 public class MyAdmin extends DeviceAdminReceiver {
    6 
    7 }

    2.清单文件中的配置:

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

    3.在res文件建立xml/my_admin.xml文件:

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <device-admin xmlns:android="http://schemas.android.com/apk/res/android">
     3     <!-- 
     4     limit-password  设置密码的规则
     5     watch-login 监控屏幕解锁尝试次数
     6     reset-password 更改屏幕解锁密码
     7     force-lock 设备自动锁屏
     8     wipe-data 删除全部的数据
     9     
    10      -->
    11     <uses-policies>
    12         <limit-password/>
    13         <watch-login />
    14         <reset-password />
    15         <force-lock />
    16         <wipe-data />
    17     </uses-policies>
    18 </device-admin>

    4.MainActivity代码:

     1 package com.example.lockscreen;
     2 
     3 import android.app.Activity;
     4 import android.app.admin.DevicePolicyManager;
     5 import android.content.ComponentName;
     6 import android.content.Context;
     7 import android.content.Intent;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 
    11 public class MainActivity extends Activity {
    12 
    13     @Override
    14     protected void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.activity_main);
    17     }
    18 
    19     public void onClick(View v){
    20         // 设备安全管理服务    2.2之前的版本是没有对外暴露的 只能通过反射技术获取  
    21         DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    22         
    23         // 申请权限
    24         ComponentName componentName = new ComponentName(this, MyAdmin.class);
    25         // 判断该组件是否有系统管理员的权限
    26         boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
    27         if(isAdminActive){
    28             
    29             devicePolicyManager.lockNow(); // 锁屏
    30             
    31             devicePolicyManager.resetPassword("123", 0); // 设置锁屏密码
    32 //            devicePolicyManager.wipeData(0);  恢复出厂设置  (建议大家不要在真机上测试) 模拟器不支持该操作
    33             
    34         } else {
    35             Intent intent = new Intent();
    36             // 指定动作名称
    37             intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    38             // 指定给哪个组件授权
    39             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
    40             startActivity(intent);
    41         }
    42         
    43         
    44     }
    45 }

     注:如果是在2.2版本之前的话,那么必须用反射来解决,

     1 DevicePolicyManager mService = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
     2 Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
     3 IBinder binder = (IBinder) method.invoke(null,new Object[] { Context.DEVICE_POLICY_SERVICE });
     4 mService = IDevicePolicyManager.Stub.asInterface(binder);
     5 
     6 
     7 
     8 3.注册广播接受者为admin设备
     9 ComponentName mAdminName = new ComponentName(this, MyAdmin.class);
    10 if (mService != null) {
    11         if (!mService.isAdminActive(mAdminName)) {
    12                     Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    13                     intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName);
    14                     startActivity(intent);
    15                 }
    16 }
  • 相关阅读:
    【LeetCode每天一题】Rotate List(旋转链表)
    【LeetCode每天一题】Permutation Sequence(排列序列)
    【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)
    【LeetCode每天一题】Merge Intervals(合并区间)
    【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
    Ajax基础
    git的命令行操作
    新闻发布系统之登录和注销
    JSTL和EL
    servlet模板的修改
  • 原文地址:https://www.cnblogs.com/androidez/p/2915020.html
Copyright © 2020-2023  润新知