码迷,mamicode.com
首页 > 其他好文 > 详细

设备管理器进行锁屏和数据清除等功能

时间:2016-12-25 01:49:10      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:example   systems   filter   vertica   enable   one   idg   odi   mis   

 

 1.首先创建一个类DeviceAdminSample继承DeviceAdminReceiver(android.app.admin.DeviceAdminReceiver),作为接收广播信息的处理类(其实不用实现,主要内部也是维护了DevicePolicyManager类的实例,主要操作的功能是这个类的内部方法实现的)

package com.example.deviceadmi;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;

public class DeviceAdminSample extends DeviceAdminReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
}

2.在清单配置文件上配置receiver信息

<receiver
            android:name="com.example.deviceadmi.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="@layout/device_admin_sample" /><!--device_admin_sample的xml文件需要创建-->

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

 3.创建device_admin_sample.xml文件

<?xml version="1.0" encoding="utf-8"?>
<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>

 4.在activity上直接创建调用就可以

package com.example.deviceadmi;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private ComponentName mDeviceAdminSample;
    private DevicePolicyManager mDPM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btn_active=(Button) findViewById(R.id.btn_active);
        Button btn_lock=(Button) findViewById(R.id.btn_lock);
        Button btn_wipedata=(Button) findViewById(R.id.btn_wipedata);
        Button btn_unintall=(Button) findViewById(R.id.btn_unintall);
        
        mDeviceAdminSample = new ComponentName(this, DeviceAdminSample.class);
        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        
        btn_active.setOnClickListener(new OnClickListener() {
            
            //调用设备管理器的页面显示,激活需要自己点击
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"设备管理器");
                startActivity(intent);
            }
        });
        
        btn_lock.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (mDPM.isAdminActive(mDeviceAdminSample)) {//判断定义好的组件是否开启激活了
                    mDPM.lockNow();
                    mDPM.resetPassword("123", 0);
                }else{
                    Toast.makeText(getApplicationContext(), "还没有激活设备管理器", Toast.LENGTH_LONG).show();
                }
            }
        });
        
        btn_wipedata.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO 模拟器是没有进行数据的清除的,真机才会清除数据
                if (mDPM.isAdminActive(mDeviceAdminSample)) {
                    mDPM.wipeData(0);//清除手机内部存储的数据
//                    mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除手机外部存储的数据
                } else {
                    Toast.makeText(getApplicationContext(), "还没有激活设备管理器", Toast.LENGTH_LONG).show();
                }
            }
        });
        
        
        /**
         * 卸载页面的调用
         * <activity android:name=".UninstallerActivity"
                android:configChanges="orientation|keyboardHidden"
                android:theme="@style/TallTitleBarTheme">
            <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>
        </activity>
         */
        btn_unintall.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.DELETE");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse("package:"+getPackageName()));//卸载当前的应用
                startActivity(intent);
            }
        });
        
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 activity_main.xml布局文件

 

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.deviceadmi.MainActivity" >

    <Button 
        android:id="@+id/btn_active"
        android:text="一键开启"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btn_lock"
        android:text="一键锁屏"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btn_wipedata"
        android:text="一键清除数据"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btn_unintall"
        android:text="一键卸载"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

 

运行结果:

 技术分享  技术分享  技术分享  技术分享

 

 

参考 Device Administration

 技术分享

设备管理器进行锁屏和数据清除等功能

标签:example   systems   filter   vertica   enable   one   idg   odi   mis   

原文地址:http://www.cnblogs.com/2016dreamchen/p/6218788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!