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.
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);
What I’ve Tried:
-
Using context in onReceive to call the getWindow but it doesnt work.
public void onReceive(Context context, Intent intent) {
int eventId = intent.getIntExtra("eventId",0); String comments = intent.getStringExtra("comments"); String alarm_title = 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);
-
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);// Set flags to wake up the 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); // Optionally, you can set a layout for this activity setContentView(R.layout.activity_wake_screen); // Finish the activity after some time or based on your needs // This example finishes it after 5 seconds new Handler().postDelayed(this::finish, 5000); }
}
-
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