有两个东西弱锁和wifimanager你也可以使用任何一个来防止进入睡眠模式
PowerManager pm;
PowerManager.WakeLock wakeLock, wakeLock_deamScreen,wakeLock_Full_wake_lock;
WifiLock lock;
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"SleepMode by pressing Power Button");
wakeLock.acquire();
wakeLock_deamScreen = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE| PowerManager.ACQUIRE_CAUSES_WAKEUP,"Screen Deam or screen stays on for a little longer");
wakeLock_deamScreen.acquire();
wakeLock_Full_wake_lock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,"");
wakeLock_Full_wake_lock.acquire();
ComponentName receiver = new ComponentName(this,EyeSpyliteActivity.class);
PackageManager packagemanager = getPackageManager();
packagemanager.setComponentEnabledSetting(receiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag");
lock.acquire();
并且不要忘记释放它 onDestroy
@Override
protected void onDestroy() {
lock.release();
wakeLock.release();
wakeLock_deamScreen.release();
wakeLock_Full_wake_lock.release();
}