android wake lock 电源管理简单学习

 需要配置清单文件:<uses-permission android:name="android.permission.WAKE_LOCK" />

也可以参考我之前写的这篇文章:

http://blog.csdn.net/aikongmeng/article/details/39232017

package com.example.wakeup;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener{
	private static final String TAG = "MainActivity";
	private static final String WAKE_LOCK_TAG = "LightTag";
	private PowerManager pm ;
	private PowerManager.WakeLock wl;
	private TextView tv;
	private StringBuilder mBuilder = new StringBuilder(2048);
	private SensorManager mSensorManager;
	private Sensor mLight;
	private Handler handler =new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
	}


	@Override
	protected void onResume() {
		super.onResume();
		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
		wl.acquire();  
	}


	@Override
	protected void onStart() {
		super.onStart();
		mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_FASTEST);
		//register broadcasts receiver
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);  
		registerReceiver(mReceiver, filter);  

	}
	@Override
	protected void onPause() {
		super.onPause();
		//release lock
		if (wl!=null) {
			wl.release();
			wl=null;
		}

	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mSensorManager.unregisterListener(this);
		unregisterReceiver(mReceiver);  

	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		Log.i(TAG, "Light Values :"+ event.values[0]);
		mBuilder.insert(0,  event.values[0]+"\n");
		tv.setText(mBuilder.toString());
		tv.invalidate(); 
	}
	private BroadcastReceiver mReceiver =  new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(intent.getAction())) {
				handler.post(new Runnable() {  
					@SuppressLint("Wakelock")
					public void run() {  
						if (wl != null) {
							wl.release();  
							wl=null;
						} 
						try {
							System.out.println("Sleeping ...");
							Thread.sleep (3000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						wl = pm.newWakeLock(  
								PowerManager.PARTIAL_WAKE_LOCK  
								,  
								WAKE_LOCK_TAG);  
						wl.acquire();  
					}  
				});   
			} 
		}
	};

}

  

posted @ 2014-09-22 23:52  一个学渣  阅读(528)  评论(0编辑  收藏  举报

 

CSDNGitHub 联系邮箱:aikongmeng@gmail.com


Copyright 2008-2019 Government All Rights Reserved