PowerOff screen recovery :
/**
* Usage :
*
* onResume()
* mScreenOffWakeupReceiver = new ScreenOffWakeupReceiver();
* // optional if we should return to a specific activity
* mScreenOffWakeupReceiver.setWakeupActivity( getPackageManager().getLaunchIntentForPackage(getPackageName()));
* registerReceiver(mScreenOffWakeupReceiver, mScreenOffWakeupReceiver.getFilters());
*
* onPause()
* if (mScreenOffWakeupReceiver!=null)
* unregisterReceiver( mScreenOffWakeupReceiver );
*
* Requires Permission: android.permission.WAKE_LOCK
*/
public class ScreenOffWakeupReceiver extends BroadcastReceiver {
private static final String TAG = "ScreenOffWakeupRcvr";
public static final String ACTION_COLD_WATER = "com.buzztime.intent.action.COLD_WATER";
private static Intent wakeupIntent = null;
public void setWakeupActivity( Intent intent ){
wakeupIntent = new Intent(intent);
wakeupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK);
}
public IntentFilter getFilters() {
return new IntentFilter(){{
addAction(Intent.ACTION_SCREEN_OFF);
addAction(ACTION_COLD_WATER);
}};
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, action);
if (Intent.ACTION_SCREEN_OFF.equals(action)) {
// Screen off indicated..
// Use AlarmManager to wake the unit up again
Intent i = new Intent(ACTION_COLD_WATER)
.setClass(context, ScreenOffWakeupReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 192837, i, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 23 ){
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 500, sender);
} else
if (Build.VERSION.SDK_INT >= 19){
am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 750, sender);
} else {
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 500, sender);
}
} else if (ACTION_COLD_WATER.equals(action)) {
// after a short pause, AlarmManager will wake us up and let us run our activity
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (!pm.isInteractive()) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE , "wlKiosk");
wl.acquire(10000);
PowerManager.WakeLock wlCpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "wlKioskCpu");
wlCpu.acquire(10000);
wlCpu.release();
}
if (wakeupIntent!=null) {
context.startActivity( wakeupIntent );
}
}
}
}