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(); } }); } } }; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。