android 設備管理屏幕鎖定實現方法

android 實現屏幕鎖定需要用戶手工去激活設備管理

直接貼代碼瞭

MainActivity.java

package com.example.lockscreen;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 主Activity類
 * 作為初始UI等
 * @author wangwei
 *
 */
public class MainActivity extends Activity {

	private Button enableDevice,disableDevice,lockScreen;
	/*
	 * 這是設備管理的主類。通過它可以實現屏幕鎖定、屏幕亮度調節、出廠設置等功能。
	 */
	private DevicePolicyManager dpm;
	/*
	 * componentName可以用於啟動第三方程序的代碼
	 */
	private ComponentName componentName;
	
	/**
	 * 打印日志信息
	 * @param msg
	 * 			要打印信息
	 */
	private void log(String msg){
		Log.i("AdminReceiver", msg);
	}
	
	/**
	 * 顯示提示信息
	 * @param context
	 * @param msg
	 */
	private void showToast(Context context, String msg){
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * 初始化
	 */
	private void init(){
		log("初始化");
		enableDevice = (Button) findViewById(R.id.enableDevice);
		disableDevice = (Button) findViewById(R.id.disableDevice);
		lockScreen = (Button) findViewById(R.id.lockScreen);
		
		enableDevice.setOnClickListener(listener);
		disableDevice.setOnClickListener(listener);
		lockScreen.setOnClickListener(listener);
		
		
		//獲取系統服務
		dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
		componentName = new ComponentName(this, AdminReceiver.class);
	}
	
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.enableDevice:
				//激活設備管理
				enableDevice();
				break;
			case R.id.disableDevice:
				//禁用設備管理
				disableDevice();
				break;
			case R.id.lockScreen:
				//鎖屏(調用系統鎖屏)
				lockScreen();
				break;
			default:
				break;
			}
		}
	};
	
	/**
	 * 激活設備管理
	 */
	private void enableDevice(){
		Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
		intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "測試。。。說明。。。");
		startActivityForResult(intent, 0);
	}
	
	/**
	 * 禁用設備管理
	 */
	private void disableDevice(){
		boolean bActive = dpm.isAdminActive(componentName);
		if(bActive){
			dpm.removeActiveAdmin(componentName);
		}
	}
	
	/**
	 * 鎖屏(調用系統鎖屏)
	 */
	private void lockScreen(){
		boolean bActive = dpm.isAdminActive(componentName);
		if (bActive) {
			dpm.lockNow();
		}
	}
	
}

AdminReceiver.java

package com.example.lockscreen;

import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * 設備管理消息接收類
 * @author wangwei
 *
 */
public class AdminReceiver extends DeviceAdminReceiver {

	/**
	 * 打印日志信息
	 * @param msg
	 * 			要打印信息
	 */
	private void log(String msg){
		Log.i("AdminReceiver", msg);
	}
	
	/**
	 * 顯示提示信息
	 * @param context
	 * @param msg
	 */
	private void showToast(Context context, String msg){
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public DevicePolicyManager getManager(Context context) {
		log("調用瞭getManager方法");
		return super.getManager(context);
	}
	
	@Override
	public ComponentName getWho(Context context) {
		log("調用瞭getWho方法");
		return super.getWho(context);
	}
	
	/**
	 * 禁用
	 */
	@Override
	public void onDisabled(Context context, Intent intent) {
		log("調用瞭onDisabled方法");
		showToast(context, "禁用設備管理");
		super.onDisabled(context, intent);
	}
	
	@Override
	public CharSequence onDisableRequested(Context context, Intent intent) {
		log("調用瞭onDisableRequested方法");
		return super.onDisableRequested(context, intent);
	}
	
	/**
	 * 激活
	 */
	@Override
	public void onEnabled(Context context, Intent intent) {
		log("調用瞭onEnabled方法");
		showToast(context, "啟動設備管理");
		super.onEnabled(context, intent);
	}
	
	@Override
	public void onPasswordChanged(Context context, Intent intent) {
		log("調用瞭onPasswordChanged方法");
		super.onPasswordChanged(context, intent);
	}
	
	@Override
	public void onPasswordFailed(Context context, Intent intent) {
		log("調用瞭onPasswordFailed方法");
		super.onPasswordFailed(context, intent);
	}
	
	@Override
	public void onPasswordSucceeded(Context context, Intent intent) {
		log("調用瞭onPasswordSucceeded方法");
		super.onPasswordSucceeded(context, intent);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		log("調用瞭onReceive方法");
		super.onReceive(context, intent);
	}
	
	@Override
	public IBinder peekService(Context myContext, Intent service) {
		log("調用瞭peekService方法");
		return super.peekService(myContext, service);
	}
}

發佈留言