在应用程序外部显示模态消息并在一段时间后自行关闭

Show a modal message outside application and close itself after some time

我正在寻找伪代码或真实代码。

我的目标是在两个边界之间随机显示一个模态消息框(如 "stay on top" 和 "not circumventable"),例如 t_in_minutes_minimumt_in_minutes_maximum总是相同的内容。

如果我不点击确定,消息应该不会堆积,但会在一段时间后自行关闭。

  1. 有没有办法用警报管理器做到这一点?
  2. 如何在我的应用程序外部显示模态消息?
  3. 如何让我的消息在一段时间后自动关闭而无需点击 okay?

PS:我试了很多。

制作一致的警报管理器服务 class,并研究 alert.dialogue activity。通过框显示消息后,选中

下面的 link

并调用 finish();关闭消息框的功能。

您可以使用 WindowManager api

在您的应用程序外部创建警报 window

What is WindowManager in android?

代码片段

WindowManager.LayoutParams p = new WindowManager.LayoutParams(
// Shrink the window to wrap the content rather than filling the screen 
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
// Display it on top of other application windows, but only for the current user
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
// Don't let it grab the input focus
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
// Make the underlying application window visible through any transparent parts
PixelFormat.TRANSLUCENT);
// Define the position of the window within the screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;

WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, p);

将此添加到清单

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

对于自动关闭消息,您可以使用处理程序在时间增量后关闭。