【问题标题】:From Notification: AlertDialog without UI来自通知:没有 UI 的 AlertDialog
【发布时间】:2011-05-16 21:58:49
【问题描述】:

我正在创建一个触发 Intent 的通知。这是我的代码的一个非常简短的摘录......

Notification notification = new Notification(R.drawable.icon, "notification", System.currentTimeMillis());
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(BackgroundService.this, ConnectionHandler.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, getString(R.string.notification_title), getString(R.string.notification_body), pendingIntent);
notification.flags |= notification.FLAG_AUTO_CANCEL;
nm.notify(1, notification);

在我的意图 (ConnectionHandler.class) 中,我想展示一个有效的 AlertDialog。但我希望在不打开新 UI 窗口的情况下显示 AlertDialog。对我来说最好的是,如果在点击通知条目时 AlertDialog 只是简单地出现而没有任何其他内容。

任何想法都值得赞赏。

问候,托比

【问题讨论】:

  • 谢谢@henrik。来晚了,还没看。

标签: android user-interface notifications android-intent


【解决方案1】:

来自Dev Guide

显示对话框

对话框总是作为 Activity 的一部分创建和显示。

简单的替代方法是创建一个非常基本的Activity,除了对话框之外什么都不显示,并在对话框关闭后立即调用finish()

【讨论】:

  • 您还可以为该 Activity 设置一个半透明的主题,以防止显示对话框以外的任何内容。
  • 呸,对话框是活动的一部分,这很糟糕。但是被解雇后的finish()ing 成功了。非常感谢!
【解决方案2】:

创建透明的Activity,如下所示:

How do I create a transparent Activity on Android?

并将其与对话框主题相结合。

【讨论】:

  • 啊……不错!我确定我曾经在应用程序中看到过它......所以这完全是关于设置活动的样式。也谢谢你!也许我会将它重建为这个解决方案。
  • 谢谢,但我认为 Aleadam 的解决方案更好:)
【解决方案3】:

有一个非常简单的答案:Activity CAN 看起来像一个对话框。在清单中添加:

android:theme="@android:style/Theme.Dialog"

它看起来像一个对话框。

如果您想显示警报对话框,请使用以下代码:

  • 活动
    AlertDialog d = new AlertDialog.Builder(this).setTitle("Blah-Blah").setBlah().show()
  • 清单
    android:theme="@android:style/Theme.Translucent"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多