0

I am creating an alarm app where I want to display an overlay even when the app is locked (screen lock). It can also be the case that the screen is off.

I am having an background fetch running at a interval of 25 minutes. At every 25 minutes, I am checking if there is any alarm which needs to be rung (less than 25 minutes). Then I am using AndroidAlarmManager to the minutes left and then adding a callback with in turn plays the default song. I am using system_alert_window to show the overlay. Now, I want to wake up the device and show the overlay (even when there is a screen lock).

Any ideas how I can wake up the screen and show the overlay?

TheFishTheySay
  • 190
  • 2
  • 17

1 Answers1

1

For waking the device you need to use this,

https://stackoverflow.com/a/10222999/7006744

also, you need to define permission

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

before displaying an overlay, you need to ask permission to overlay by using the below code.

startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
            Uri.parse("package:" + getPackageName())).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

At last, you need to set BroadcastReceiver to PendingIntent of AlarmManager. By using the below code.

Intent alarmIntent = new Intent(context, BroadcastReceiver.class)
            .setAction(ALARM_ACTION);
    PendingIntent pending = PendingIntent.getBroadcast(context,
            ALARM_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_MUTABLE);
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

you need to define in the activity in manifest code,

<intent-filter>
      <category android:name="android.intent.category.LAUNCHER" />
      <category android:name="android.intent.category.HOME" />
      <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

In the BroadcastReceiver, you can call intent for overlay view

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (Settings.canDrawOverlays(context)) {
        context.startActivity(new Intent().setClassName(context.getPackageName(), context.getPackageName() + ".OverlayActivity")
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
} else {
    context.startActivity(new Intent().setClassName(context.getPackageName(), context.getPackageName() + ".OverlayActivity")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
Vivek Modi
  • 117
  • 1
  • 4