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

一键锁屏

时间:2015-06-01 22:55:58      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

设备策略服务   DevicePolicyManager

要配置特殊的广播接受者

<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"
    tools:context=".MainActivity" >
    
     <Button
        android:onClick="openAdmin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开启管理员权限" />

    <Button
        android:onClick="lockscreen"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一键锁屏" />
    
    <Button
        android:onClick="uninstall"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="卸载软件" />

</RelativeLayout>



package com.itheima.lockscreen;

import android.app.admin.DeviceAdminReceiver;

/**
 * 特殊的广播接收者
 * @author Administrator
 *
 */
public class MyAdmin extends DeviceAdminReceiver {

}



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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.lockscreen.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name="com.itheima.lockscreen.MyAdmin"
            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>



package com.itheima.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 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);
		
	}
	/**
	 * 要在设备管理器哪里激活一下,可以用代码激活
	 * 用代码去开启管理员
	 * @param view
	 */
	public void openAdmin(View view){
		//创建一个Intent 
		Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		//我要激活谁
		ComponentName   mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
		
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
       //劝说用户开启管理员权限
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
               "哥们开启我可以一键锁屏,你的按钮就不会经常失灵");
        startActivity(intent);
	}
	
	/**
	 * 一键锁屏
	 */
	
	public void lockscreen(View view){
		ComponentName   who = new ComponentName(this,MyAdmin.class);
		if(dpm.isAdminActive(who)){
			dpm.lockNow();//锁屏
			dpm.resetPassword("", 0);//设置屏蔽密码 ""就是取消掉密码
			
			//清除Sdcard上的数据
//			dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
			//恢复出厂设置
//			dpm.wipeData(0);
		}else{
			Toast.makeText(this, "还没有打开管理员权限", 1).show();
			return ;
		}
	
		
	}
	
	/**
	 * 卸载当前软件
	 */
	
	public void uninstall(View view ){
		
		//1.先清除管理员权限
		ComponentName   mDeviceAdminSample = new ComponentName(this,MyAdmin.class);
		dpm.removeActiveAdmin(mDeviceAdminSample);
		//2.普通应用的卸载
		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.setData(Uri.parse("package:"+getPackageName()));
		startActivity(intent);
	}

	

}



一键锁屏

标签:

原文地址:http://my.oschina.net/u/2356176/blog/423635

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