public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(MainActivity.this, MyService.class); startService(intent); } }
然后就是MyService.java 主要内容
public class MyService extends Service { private SensorManager mManager; private Sensor mSensor = null; private SensorEventListener mListener = null; private PowerManager localPowerManager = null; private PowerManager.WakeLock localWakeLock = null; @Override public void onCreate() { //获取系统服务POWER_SERVICE,返回一个PowerManager对象 localPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag localWakeLock = this.localPowerManager.newWakeLock(32, "MyPower"); //获取系统服务SENSOR_SERVICE,返回一个SensorManager对象 mManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //获取距离感应器对象 mSensor = mManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); //注册感应器事件 mListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { float[] its = event.values; if (its != null && event.sensor.getType() == Sensor.TYPE_PROXIMITY) { System.out.println("its[0]:" + its[0]); //经过测试,当手贴近距离感应器的时候its[0]返回值为0.0,当手离开时返回1.0 if (its[0] == 0.0) {// 贴近手机 System.out.println("手放上去了..."); if (localWakeLock.isHeld()) { return; } else localWakeLock.acquire();// 申请设备电源锁 } else {// 远离手机 System.out.println("手拿开了..."); if (localWakeLock.isHeld()) { return; } else localWakeLock.setReferenceCounted(false); localWakeLock.release(); // 释放设备电源锁 } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; } @Override public int onStartCommand(Intent intent, int flags, int startId) { //注册监听 mManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { //取消监听 mManager.unregisterListener(mListener); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return null; } }
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("android.intent.action.SCREEN_OFF")) { } else if (action.equals("android.intent.action.SCREEN_ON")) { } } }
/** * Get a wake lock at the level of the flags parameter. Call * {@link WakeLock#acquire() acquire()} on the object to acquire the * wake lock, and {@link WakeLock#release release()} when you are done. * * {@samplecode *PowerManager pm = (PowerManager)mContext.getSystemService( * Context.POWER_SERVICE); *PowerManager.WakeLock wl = pm.newWakeLock( * PowerManager.SCREEN_DIM_WAKE_LOCK * | PowerManager.ON_AFTER_RELEASE, * TAG); *wl.acquire(); * // ... *wl.release(); * } * * @param flags Combination of flag values defining the requested behavior of the WakeLock. * @param tag Your class name (or other tag) for debugging purposes. * * @see WakeLock#acquire() * @see WakeLock#release() */ public WakeLock newWakeLock(int flags, String tag) { return new WakeLock(flags, tag); }
这个方法将创建WakeLock对象,通过调用此对象的方法你就可以方便的去控制电源的状态。方法如下:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire(); 屏幕将停留在设定的状态,一般为亮、暗状态 wl.release(); 释放掉正在运行的cpu或关闭屏幕。