Chris Damianidis
Chris Damianidis

Reputation: 79

How to Wake Up the Screen in BroadcastReceiver After PowerManager.FULL_WAKE_LOCK Deprecation?

I'm working on an Android app where I need to wake up the device screen when an alarm is triggered. Previously, I used PowerManager.FULL_WAKE_LOCK to achieve this, but it has been deprecated.

The way I used to wake up the screen:

        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "CrmAlarmReceiver::WakeLock");
    try {
        wakeLock.acquire(5000); // 5 seconds
        // Your existing code...
    } finally {
        if (wakeLock.isHeld()) {
            wakeLock.release();
        }
    }

I found this approach but I need to extend Activity to use the getWindow(), so while my AlarmReceiver extends BroadcastReceiver it doesnt work.

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

Here is what I tried:

  1. Using getWindow() in BroadcastReceiver:

    public void onReceive(Context context, Intent intent) {
    
        int eventId = intent.getIntExtra("eventId", 0);
    
        String comments = intent.getStringExtra("comments");
    
        String alarmTitle = context.getString(R.string.app_name);
    
        NOTIFICATION_ID = eventId;  
      context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);             context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);               context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    
    }
    
  2. Starting a new activity with the flags set, but the activity does
    not run the code.

    Intent activityIntent = new Intent(context, AlarmActivity.class);
    
    activityIntent.putExtra("eventId", eventId);
    
    activityIntent.putExtra("comments", comments);
    
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    
    context.startActivity(activityIntent);
    

   public class WakeScreenActivity extends Activity {
   
 @Override
    protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);        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);
        setContentView(R.layout.activity_wake_screen);   
        new Handler().postDelayed(this::finish, 5000);
    }}
  1. Setting as fullscreenintent an intent that has the above code inside.

    private PendingIntent createFullScreenIntent(Context context, int eventId, String comments) {
    Intent fullScreenIntent = new Intent(context, WakeScreenActivity.class);
    fullScreenIntent.putExtra("eventId", eventId);
    fullScreenIntent.putExtra("comments", comments);
    return PendingIntent.getActivity(context, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);    }
    

What is the recommended approach for waking up the screen and ensuring it is visible when an alarm is triggered, given the deprecation of PowerManager.FULL_WAKE_LOCK?

Target SDK version: 34 Minimum SDK version: 21

Upvotes: 0

Views: 35

Answers (0)

Related Questions