Java – launch the application through the always on screen and lock screen

I have an application that starts itself if it receives a message

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. Oncreate in Java – >

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 a message from the user of my application saying that this does not apply to his Samsung Galaxy S7. He noticed that he used the "always on display" function

I searched the Internet for some time, but I couldn't find a solution Are there any more "tags" to add?

Solution

I have a similar problem The flags you added to intent do not apply to intent, but for window – you do not need to add them there You can use them correctly in oncreate() But the problem is not the flag

In my case, the problem is that the activity I tried to start uses a theme with < item name = "Android: windowsisfloating" > true < / item > I rewritten my logic, so I don't have to use it If you absolutely have to use "Android: windows is floating", another solution is to use

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

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

Then you can call

screenWakeLock.acquire(5000);

Before setting the flag You can edit the timeout, of course This is a bit like a hacker because PowerManager is not recommended SCREEN_ DIM_ WAKE_ LOCK. But it applies to Samsung with Android 8.0 and Samsung experience 9.0 The whole thing is obviously a Samsung problem, because there is no workaround for the same code, just like the charm of a pixel ambient display that is always on

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>