How do I create an Activity that is visible on top of the lock screen

99封情书 提交于 2019-12-03 02:37:48

This is not possible as far as I know. It sounds like you are trying to replicate the iOS experiences. You should realize that android has its own conventions and violating them is not something you should do lightly.

Android is a little bit of a contradiction. It's very open and as a developer you have access to anything, and it's up to you to use those powers for good or evil. When I say evil I don't mean malware. I mean apps that try to get cute and use things in ways they weren't meant to be used, like putting up notifications asking you to use the app more. The contradiction is that you don't actually have access to everything, there are a few parts the developers decided were so important that app couldn't mess with them. The lock screen is one of those parts. You can replace your home app all you want, but you never have to worry about your replacement lock screen failing and preventing you from accessing your phone.

Even if this were possible you would have more problems to deal with. Every lock screen is different, manufacturers can and do customize it so you have no guarantees your activity won't get in the way of unlocking the phone.

Uses the flags is the right way to do it, using the power manager is wrong.

Your requests sound conflicting to me: you say you want the activity to appear on top of the lock screen (in fact we don't do that, we hide the lock screen so the activity can be seen), while at the same time you want the user to first have to unlock the device.

If you are thinking you want the user to see a notification before unlocking the device to see your activity... I really think you don't want that. The notification is very small (in the status bar at the top), and the next that is shown while posting it is very transient. This is not going to be a good experience for someone who heard their phone beep or buzz and is pulling it out to see what is going on.

You should use whatever combination of the window flags that make sense for your app. You can get pretty much any reasonable behavior between the various combinations of them. These are used for the alarm clock, incoming call UI, etc.

Try this in your activity

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

    boolean screenOn = pm.isScreenOn();//check id screen is on

    if(!screenOn){//if not turn it on or wkaeup the screen

        final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Screen On");
        wl.acquire();
        Toast.makeText(getBaseContext(), "This is WAKEUP SCREEN", Toast.LENGTH_SHORT).show();
        Thread timer = new Thread(){
            public void run(){
                try {
                    sleep(5000);
                } catch (InterruptedException e) {
                    // TODO: handle exception
                }finally{
                    wl.release();// release wakelock important
                }
            }
        };
        timer.start();
    }

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);//keep the activity running under lock screen..

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!