实现自定义锁定屏幕时出现问题(后台活动问题)

发布于 2025-01-02 04:42:37 字数 1549 浏览 1 评论 0原文

我正在构建一个带有自定义锁屏的自定义主屏幕。

当屏幕关闭时,我启动锁定屏幕(活动), 但是,当锁定屏幕被终止(通过“finish()”)时,它会返回到 我的主屏幕 apk 中的最后一个活动,而不是真正的活动 (apk) 在屏幕关闭之前就可见。

例如,如果我在计算器应用程序或时钟应用程序中,并且锁定屏幕打开,当锁定屏幕活动完成时,它不会返回到计算器/时钟

这是我注册锁定屏幕的位置(在主启动器活动)用于接收屏幕开/关事件:

private void doLockScreenOperations()
{
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();

    IntentFilter lockfiFilter = new IntentFilter();
    lockfiFilter.addAction(Intent.ACTION_SCREEN_OFF);
    lockfiFilter.addAction(Intent.ACTION_SCREEN_ON);
    getApplicationContext().registerReceiver(new LockScreenReceiver(), lockfiFilter);
}

这是接收器本身,我在其中启动锁定屏幕的活动:

public class LockScreenReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equals(Intent.ACTION_SCREEN_OFF))
        {
            if (LockScreenActivity.isLockScreenAlive == false)
            {
                Intent lockIntent = new Intent(context, LockScreenActivity.class);
                lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(lockIntent);
            }
        }

        else if (action.equals(Intent.ACTION_SCREEN_ON))
        {

        }
    }
}

此时,LockScreenActivity 是一个带有按钮的简单活动 单击按钮时调用 finish()。

我不知道如何解决这个问题。

提前致谢!

Im building a custom home screen with a custom lockscreen.

When the screen turn off, I launch the lock screen (activity),
However, when the lock screen is killed (by "finish()"), it goes back to
the last activity in my homescreen apk, and not to the real activity (apk) that
was visible right before the screen went off.

For example, if i'm in Calculator application, or in Clock applicaiton, And the lock screen turns on, When the lock screen activity is finished, It doesn't return to Calculator/Clock

Here's where I register the lock screen (in the main launcher activity) for receiving screen on/off events:

private void doLockScreenOperations()
{
    KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();

    IntentFilter lockfiFilter = new IntentFilter();
    lockfiFilter.addAction(Intent.ACTION_SCREEN_OFF);
    lockfiFilter.addAction(Intent.ACTION_SCREEN_ON);
    getApplicationContext().registerReceiver(new LockScreenReceiver(), lockfiFilter);
}

Here's the receiver itself, where I launch the lock screen's activity:

public class LockScreenReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();

        if (action.equals(Intent.ACTION_SCREEN_OFF))
        {
            if (LockScreenActivity.isLockScreenAlive == false)
            {
                Intent lockIntent = new Intent(context, LockScreenActivity.class);
                lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(lockIntent);
            }
        }

        else if (action.equals(Intent.ACTION_SCREEN_ON))
        {

        }
    }
}

The LockScreenActivity is, at that moment, a simple activity with a button
that is called finish() when the button is clicked.

I have no idea how to fix this.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2025-01-09 04:42:37

我不完全确定,伙计,但你必须考虑这一点:
Intent.FLAG_ACTIVITY_NEW_TASK 启动一组全新的视图
因此从逻辑上讲,退出锁定屏幕时您没有可以返回到之前的活动。

我正在自己构建一个,与您实际的做法非常相似。
但不幸的是有这些问题:
-有时加载速度很慢
- 只要“感觉”像这样,它就会加载:/
nm,祝你好运,伙计

I'm not entirely sure mate, but you have to consider this:
Intent.FLAG_ACTIVITY_NEW_TASK starts a fresh new group of views
so logically you don't have a previous activity to go back to when exiting your lock screen.

I'm building one myself, very similarly to how you do it actually.
but unfortunately having these problems:
-it loads to slow sometimes
-it loads whenever it "feels" like :/
nm, goodluck mate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文