如何使活动窗口始终位于顶部

发布于 2024-10-04 07:11:22 字数 74 浏览 0 评论 0原文

我想创建一个始终位于其他活动顶部的活动(如 Windows 中的模式窗口或任务管理器)。 我如何在 Android 上执行此操作?谢谢

I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities.
How can I do this on Android? Thanks

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

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

发布评论

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

评论(4

所有深爱都是秘密 2024-10-11 07:11:22

您可以在您的 Activity 的重写 onStop 方法中使用以下代码:

@Override
protected void onStop(){
    super.onStop();
    Intent intent = new Intent(this, ClassNameOfYourActivity.class);
    startActivity(intent);
}

美化问题:如果任何其他 Activity 试图获取焦点,您的 Activity 将再次弹出。所以它不是一个模态窗口。

而且很危险!您将无法处理 Android GUI,您只能控制应用程序 GUI。例如,打开和关闭调试模式、终止应用程序(仅通过 ADB)、访问系统设置等都是不可能的。如果你关闭 ADB 并将其与自动启动机制结合起来,那么你就会陷入困境。

因此,如果您与 Play 分享,您将不会受欢迎:)

You can use the following code in the overridden onStop method of your activity:

@Override
protected void onStop(){
    super.onStop();
    Intent intent = new Intent(this, ClassNameOfYourActivity.class);
    startActivity(intent);
}

Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.

And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.

So you won't be popular if you share it with Play :)

猫性小仙女 2024-10-11 07:11:22

你不能。正如Android系统中定义的那样。

You can't. As this is defined in the Android system.

千と千尋 2024-10-11 07:11:22

根据您具体想要执行的操作,您可能会对位于其他活动之上的窗口感到满意。

超级视频客户端等应用; 浮动笔记能够显示浮动窗口位于其他活动之上。这些问题可能会为您指明正确的方向:

Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.

Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:

顾北清歌寒 2024-10-11 07:11:22

按照步骤实现您的要求

  1. 创建一个将成为顶级活动的活动
  2. 在清单中添加以下代码

    />
    
  3. 使用以下代码获取用户的叠加权限

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            新的AlertDialog.Builder(这个)
                .setTitle("权限请求")
                .setMessage("此应用程序需要您的许可才能覆盖系统应用程序")
                .setPositiveButton("打开设置", new DialogInterface.OnClickListener() {
                    公共无效onClick(DialogInterface对话框,int其中){
                        Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                        startActivityForResult(myIntent, 101);
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                。展示();
        }
    }
    

Follow the steps to achieve your requirement

  1. Create an activity which is going to be the top activity
  2. Add the following code in the manifest

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
  3. Use the following code to get overlay permission from user

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            new AlertDialog.Builder(this)
                .setTitle("Permission Request")
                .setMessage("This app needs your permission to overlay the system apps")
                .setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                        startActivityForResult(myIntent, 101);
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .show();
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文