android亮屏解锁demo

公司之前有个项目需要将待机的移动设备先进行亮屏和解锁再进行其他功能的操作,如是写了一个测试demo,将亮屏和解锁的代码部分写在service中,测试代码通过点击按钮,锁屏后10s发送消息启动服务,亮屏并解锁,实际项目中是通过广播的方式启动服务的.

代码展示如下:

1、布局文件wake_main.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:gravity="center_horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/wake_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/start" />

</RelativeLayout>

2、服务WakLockService.java

package cn.wakelock.demo;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

public class WakLockService extends Service {
	
	private static final String TAG="WakLockService";
	
	// 键盘管理器  
	KeyguardManager keyguardManager;
	// 键盘锁  
	private KeyguardLock keyguardLock;
	// 电源管理器  
	private PowerManager powerManager;
	// 唤醒锁  
	private PowerManager.WakeLock wakeLock;
	

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		powerManager=(PowerManager) getSystemService(Context.POWER_SERVICE);
		keyguardManager=(KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
				
	}
	
	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		 Log.e(TAG, "Service start--------------");
		 // 点亮亮屏  
		 wakeLock = powerManager.newWakeLock  
	     (PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "Tag");  
	     wakeLock.acquire();  
	     keyguardLock = keyguardManager.newKeyguardLock("");  
	     // 键盘解锁  
	     keyguardLock.disableKeyguard();  
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		 Log.e(TAG, "Service onDestroy--------------");
		 if (wakeLock != null) {  
             wakeLock.release();  
             wakeLock = null;  
         }  
         if (keyguardLock!=null) {  
             keyguardLock.reenableKeyguard();  
         }  
		
	}

}
注意服务需要添加权限且在布局文件中进行注册

   

   <uses-permission android:name="android.permission.WAKE_LOCK"/>
   <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
   <permission android:name="android.permission.DEVICE_POWER"/>
   ......
   <service android:name="cn.wakelock.demo.WakLockService"></service>

3、WakeActivity.java

package cn.wakelock.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.jon.demo.R;

public class WakeActivity extends Activity implements OnClickListener{
	
	public static final int WAKE_UNLOCK=0x1123;
	private Handler mHandler=new Handler()
	{
		public void handleMessage(android.os.Message msg) 
		{
			switch (msg.what) {
			case WAKE_UNLOCK:
				Log.e("WakeActivity", "handleMessage begin----------");
				Intent mIntent=new Intent();
				mIntent.setClass(WakeActivity.this, WakLockService.class);
				startService(mIntent);
				break;

			default:
				break;
			}
			
		};
	};
	private Button mButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wake_main);
		initViews();
		
	}

	private void initViews() {
		mButton=(Button) findViewById(R.id.wake_start);
		mButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.wake_start:
			Message msg=new Message();
			msg.what=WAKE_UNLOCK;
			mHandler.sendMessageDelayed(msg, 10000);
			break;

		default:
			break;
		}
		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.e("WakeActivity", "stop service----------");
		Intent mIntent=new Intent();
		mIntent.setClass(WakeActivity.this, WakLockService.class);
		stopService(mIntent);
	}

}

测试时,点击按钮,锁屏,10s后屏幕会通过服务亮屏并解锁,项目中需要用到上述功能的可以嵌入上面代码部分.

你可能感兴趣的:(android亮屏解锁demo)