【问题标题】:Show a modal message outside application and close itself after some time在应用程序外部显示模式消息并在一段时间后自行关闭
【发布时间】:2017-01-20 17:09:40
【问题描述】:

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

我的目标是在两个边界之间随机显示一个模态消息框(如“留在顶部”和“不可规避”),例如 t_in_minutes_minimumt_in_minutes_maximum 始终具有相同的内容。

如果我不点击 OK,消息不应该堆积起来,而是在一段时间后自行关闭。

  1. 有没有办法通过警报管理器做到这一点?
  2. 如何在我的应用程序之外显示模式消息?
  3. 如何让我的消息在一段时间后自行关闭而不点击 OK?

PS:我尝试了很多。

【问题讨论】:

  • 这样做的方法是让一个服务运行一个带有.setOngoing(true); 的前台通知,然后使用一个处理程序计时器或另一个AlarmManager 在需要时终止该服务。

标签: android algorithm alarmmanager


【解决方案1】:

您可以使用 WindowManager api 在应用程序外部创建警报窗口

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"/>

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

【讨论】:

    【解决方案2】:

    制作一致的警报管理器服务类,并研究alert.dialogue 活动。通过框显示消息后,请检查下面的此链接

    Execute function after 5 seconds in Android

    并调用finish();关闭消息框的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-25
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多