码迷,mamicode.com
首页 > 移动开发 > 详细

android wake lock 电源管理简单学习

时间:2014-09-23 02:19:23      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   android   blog   http   io   os   java   ar   文件   

 需要配置清单文件:<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();  
					}  
				});   
			} 
		}
	};

}

  

android wake lock 电源管理简单学习

标签:des   android   blog   http   io   os   java   ar   文件   

原文地址:http://www.cnblogs.com/aikongmeng/p/3987343.html

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