如何让Android上的Activity始终位于最前面?

发布于 2024-12-07 22:39:43 字数 1077 浏览 0 评论 0原文

我制作了一个锁屏应用程序,可以防止其他人在锁定时访问设备。我有一个名为锁屏活动的活动。这是代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class LockScreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lockscreen);
    }

    protected void onPause() {
            super.onPause();
            // it will display a lock screen again when the home button is pressed
            Intent myIntent = new Intent(LockScreen.this, LockScreen.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(myIntent);
        }
    }

该代码用于在按下主页按钮时重新启动锁屏活动,以便设备仍然可以锁定。如果我按下主页按钮并且没有点击任何应用程序,它将在 5 秒内再次显示锁屏活动(基于 Android 问题),但如果我点击任何应用程序(例如:设置或消息),我的锁屏活动将在我关闭该应用程序(设置或消息位于顶部并覆盖我的锁屏活动),以便仍然可以访问该设备。 有谁知道如何使活动保持领先以最大限度地减少未经授权人员的访问?谢谢..

i make a lock screen application that prevent other people to access the device when it's locked. i have an activity called lockscreen activity. here's the code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class LockScreen extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lockscreen);
    }

    protected void onPause() {
            super.onPause();
            // it will display a lock screen again when the home button is pressed
            Intent myIntent = new Intent(LockScreen.this, LockScreen.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(myIntent);
        }
    }

that code is used for relaunch the lockscreen activity when home button pressed, so that device can still locked. if i press the home button and didn't tap any application, it will show that lockscreen activity again within 5 second (based on android issue), but if i tap any application (eg: setting or messages) my lockscreen activity will shown after i close that application (the setting or messages is on the top and cover my lockscreen activity) so the device still can be accessed.
has anyone know how to make an activity stay on top for minimize the access from unauthorized people? thanks..

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

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

发布评论

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

评论(2

月亮是我掰弯的 2024-12-14 22:39:43

我不太确定是否有可能让应用程序始终保持领先地位。当他们按下主页按钮时你会做什么?或者如果他们运行 Cyanogen 并按住后退按钮“杀死”应用程序会怎样?这听起来不像是制作锁屏的有效方法。也许您可以看看其他一些类似的问题。我只是认为必须有更好的方法来实现锁屏应用程序。

Android锁屏源码

这个链接告诉你如何在线查找android源代码
哪里可以在线找到 Android 源代码?

并且这是 LockScreen.java 所在位置的实际链接
https://github。 com/android/platform_frameworks_policies_base/blob/master/phone/com/android/internal/policy/impl/LockScreen.java

祝你好运!

I'm not so sure that is possible to make an app stay on top always like that. And what are you going to do when they push the home button? Or what if they run Cyanogen and hold the back button to "kill" the app? It doesn't sound like an effective way to make a lock screen. Perhaps you can look at some of these other questions that are kinda similar. I just think that there must be a better way to implement a lock screen app.

Source of Android's lock screen

This link tells you how to find the android source code online
Where can I find Android source code online?

And this is the actual link of where the LockScreen.java is located
https://github.com/android/platform_frameworks_policies_base/blob/master/phone/com/android/internal/policy/impl/LockScreen.java

Good luck!

时光暖心i 2024-12-14 22:39:43

我很早以前就遇到过这个问题,后来我终于发现除非你修改框架,否则这是不可能的。

I have come across this problem long before, then I finally found out it's impossible to do so unless you modify framework.

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