一键锁屏一键删除设备管理器

Posted 笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键锁屏一键删除设备管理器相关的知识,希望对你有一定的参考价值。

设备管理器的操作步骤

1、创建类DeviceAdminReceiver的子类

package com.itheima62.lockscreen;

import android.app.admin.DeviceAdminReceiver;

public class DeviceAdminSample extends DeviceAdminReceiver {

}

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

<receiver
            android: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-data
                android: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,在strings.xml文件中配置字符串相关信息

<string name="activity_sample_device_admin">设备管理员</string>
<string name="sample_device_admin">管理员</string>
<string name="sample_device_admin_description">简介</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,在代码中创建设备管理器和组件

DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

//看使用场景如果不需要这个可以不写
ComponentName who = new ComponentName(this, DeviceAdminSample.class);

6,写功能

dpm.lockNow();//一键锁屏

 

源码实例

DeviceAdminSample.java

技术分享
package com.itheima62.lockscreen;

import android.app.admin.DeviceAdminReceiver;

public class DeviceAdminSample extends DeviceAdminReceiver {

}
View Code

 

AndroidManifest.xml

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima62.lockscreen"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima62.lockscreen.MainActivity"
            android:label="一键锁屏" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.itheima62.lockscreen.RemoveActivity"
            android:label="一键卸载" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        
        

        <receiver
            android: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-data
                android: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>
View Code

 

String.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>
View Code

 

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>
View Code

 

MainActivity.java

技术分享
package com.itheima62.lockscreen;

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;

    @Override
    protected 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)) {//true
            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);
            
        }
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
View Code

 

RemoveActivity.java

技术分享
package com.itheima62.lockscreen;

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;
import android.widget.Toast;

public class RemoveActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        remove(null);
        
    }
     
    /**
     * 一键卸载
     * @param v
     */
    public void remove(View v){
        //调用卸载的界面
        /* <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <action android:name="android.intent.action.DELETE" />
         <category android:name="android.intent.category.DEFAULT" />
         <data android:scheme="package" />
     </intent-filter>*/
        
        // 取消激活设备管理
        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的界面
    }
}
View Code

 

以上是关于一键锁屏一键删除设备管理器的主要内容,如果未能解决你的问题,请参考以下文章

一键搜索之Win10锁屏壁纸

树梅派实现一键锁屏

一键robot有啥用

[求助]EmpireCMS一键安装的问题,请指教,谢谢!

AI大神李沐开源新手剪辑神器!只看字幕就能剪视频,卡壳重复片段一键删除...

在 Wordpress 表单管理器中一键提交多个表单