0

I am creating a timer using the Pomodoro Technique. The fist timer will be 20 minutes and in this time I expect the screen to turn off. I would like the screen to turn back on when the timer is finished.

In my manifest I have asked for the following permissions:

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

In my timer class I get the window in onCreat():

Window win;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
    createTimer(time_work);
    win = this.getWindow();
}

In my timer's onFinish() I call wakeUp():

public void onFinish() {
    text_time.setText(R.string.done);
    if (isWork) {
       pomodoro_count++;
       text_pomodoro_count.setText(String.valueOf(pomodoro_count));
       }
    isWork = !isWork;
    onResume();
    //I have more here but removed for brevity
    wakeUp();
    }

In wakeUp() I have tried every combination of this:

private void wakeUp() {
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

Every answer that I have found is depreciated.

Funlamb
  • 551
  • 7
  • 20
  • Need some more info. Is this being called while the screen is locked? – Mars Oct 20 '17 at 01:09
  • See: https://stackoverflow.com/questions/9561320/android-how-to-turn-screen-on-and-off-programmatically – Mars Oct 20 '17 at 01:24
  • The link i referred you to says where to place the call to wakeUp(), but this method may be deprecated. There are also a few other examples, so give those a try! – Mars Oct 20 '17 at 02:15
  • Yes, the link had been deprecated for a while. I'm going to take a crack at this tomorrow. Thanks for you help. – Funlamb Oct 20 '17 at 02:33

1 Answers1

0

This needs to be done in a different way then what I was thinking.

What needs to be done is a new activity needs to be created from an alarm.

You will still need the same permissions in your manifest:

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

You will need a receiver:

public class ReceiverAlarm extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;

//This is what will be called when your alarm goes off
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Logged", "ReceiverAlarm");
    Intent i = new Intent(context, ScreenWake.class);
    context.startActivity(i);
}

//This is a method I made that will set an alarm
public void setAlarm(Context context, int timeToPop) {
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, ReceiverAlarm.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    long currentTime = SystemClock.elapsedRealtime();
    long timeToAdd = (long) timeToPop;
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            currentTime + timeToAdd, alarmIntent);
}

public void cancelAlarm(Context context) {
    Intent intent = new Intent(context, ReceiverAlarm.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(sender);
}

Then you will need a completely different activity to open when the alarm gets called through the alarm:

ScreenWake.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_alarm_wakes_screen);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

    ms = new ManagerSound(getApplicationContext());
    ms.playAlarm();
}
Funlamb
  • 551
  • 7
  • 20