android的一键锁屏与一键卸载的代码实现

一.设备管理器操作步骤

1.创建类DeviceAdminReceiver的子类
如:com.itheima62.lockscreen.DeviceAdminSample
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.配置字符串相关信息
 <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;
    @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))
        {
            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) 
    {
        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
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.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-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/AppTheme" >
        <activity
            android: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>
         <activity
            android: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>

        <receiver
            android: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-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>
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

问我学院

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值