简体   繁体   中英

Start app over "Always on Display" and lock screen

I have an app, that starts on it's own, if it recieves a message. For this I use this start parameter:

BackgroundService.java:

Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            .addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
            .addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
            .addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
            .addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    startActivity(intent);

MainActivity.java --> onCreate:

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);

Now I received from a user of my app a message, that this is not working with his "Samsung Galaxy S7", and he noticed, that he uses the "Always On Display" function.

I searched for a while in the Web, but I doesn't find a solution. Are there some more "Tags" to add?

I ran in a similar problem. The flags that you add to the Intent are not meant for an Intent, but for a Window - there is no need to add them there.. You use them correctly in the onCreate(). But the issue is not due to flags.

In my case the problem was that the Activity I was trying to launch uses a Theme, that has <item name="android:windowIsFloating">true</item> in it. I reworked my logic, so I don't have to use it. Another workaround that works if you absolutely must use the "android:windowIsFloating" is to use

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);

screenWakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "TEST");

and then you can call

screenWakeLock.acquire(5000);

before you set the flags. You can edit the timeout of course. This is a bit of a hack, because PowerManager.SCREEN_DIM_WAKE_LOCK is deprecated. But it does work on Samsung with Android 8.0 and Samsung Experience Version 9.0. The whole thing is clearly some Samsung issue, because the same code, without the workarounds works like a charm with Pixel Ambient display with Always on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM