首页 > 代码库 > android wake lock 电源管理简单学习
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(); } }); } } };}
android wake lock 电源管理简单学习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。