Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

android - Keep activity running over screen lock when the screens alseep

So i built an app that functions as a lock screen replacement. I use a broadcast receiver and a service to start my activity after Intent.ACTION_SCREEN_OFF. So that every time the user locks the screen my activity starts, then when they press the unlock button my activity is already running over the lock screen. But this only works if the user tries to wake up/unlock the phone after a short amount of time. If they wait too long the activity has vanished. I'm not sure why this is happening and what I can do to keep the activity there no matter how long the user waits to try to unlock their phone.

I thought about and tried listening for Intent.ACTION_SCREEN_ON but then there is a delay between the time when the user presses the power button on their phone to wake it up and when the app loads and shows up on the screen. During this gap the user can see the android OS

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What if you use a wakelock. For example:

@Override
public void onCreate(Bundle savedInstanceState) { 
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
    wl.acquire();
    // do your things, even when screen is off
}


@Override
protected void onDestroy() {
    wl.release();
}

You must also have a wakelock permission is AndroidManifest.xml

uses-permission android:name="android.permission.WAKE_LOCK"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...