import android.os.Bundle;
import android.view.WindowManager;
import android.app.Activity;
boolean keep = false;
Activity act;


void setup() {
  size(600, 600);
  orientation(PORTRAIT);
  background(255, 0, 0);
}

void draw() {
  background(0, 0, 255);
  textSize(24);
  textAlign(CENTER);
  if (keep) {
    text("No Sleep", width/2, height/2);
  } else {
    text("Will Sleep", width/2, height/2);
  }
}

private void KeepScreen() {
  act.runOnUiThread(new Runnable() {
    public void run() {
      act.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      keep= true;
    }
  }
  );
}

@Override
  public void onStart() {
  super.onStart();
  act = this.getActivity(); 
  if (!keep) {
    KeepScreen();
  }
}

@Override
  public void onResume() {
  super.onResume();
  if (!keep) {
    KeepScreen();
  }
}

@Override
  public void onStop() {
  super.onStop();
  keep = false;
}