• 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]+"
    ");
    		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();  
    					}  
    				});   
    			} 
    		}
    	};
    
    }
    

      

  • 相关阅读:
    安装Joomla!3
    keepalived + lvs
    systemd 服务介绍
    lvs 进阶 第二章
    lvs 初始 第一章
    iptables 最终 第四章
    bind 笔记
    iptables 扩展匹配 第三章
    iptables 认识 第二章
    iptables 初见 第一章
  • 原文地址:https://www.cnblogs.com/aikongmeng/p/3987343.html
Copyright © 2020-2023  润新知