Android - 如何在原生屏幕上显示对话框?

19
我想知道如何在Android本机屏幕上弹出对话框。我目前有一个应用程序,可以截取即将拨出的电话并停止它,然后我想弹出一个对话框,以取代拨号屏幕并警告用户其呼叫已被阻止,并允许他们从对话框中选择一些新选项。
我知道有些人会建议我使用通知,但我知道这不是正确的方法。我需要能够在电话被截取时弹出一个对话框。
以下是到目前为止我的对话框代码。
  AlertDialog LDialog = new AlertDialog.Builder(context)
     .setTitle("Call Blocked")
     .setMessage("Call Blocked, reroute call?")
     .setPositiveButton("ok", null).create();
      LDialog.show();

我认为我必须以拨号器屏幕的上下文来解决这个问题?

有人可以提供任何帮助、协助或者教程链接吗?

先感谢您的帮助。

2个回答

62

为了我的应用程序,我使用了一个带有 Dialog 主题的活动页面。 你可以在清单文件中声明这个主题:

<activity android:name="PopupActivity"
  android:launchMode="singleInstance" android:excludeFromRecents="true"
  android:taskAffinity="" android:theme="@android:style/Theme.Dialog" />
  • 如果你的弹出窗口与你的主应用程序没有关联,使用launcheMode="singleInstance"taskAffinity=""。否则用户可能会点击返回按钮并返回到应用程序的上一个活动。
  • excludeFromRecents="true"可以避免你的弹出窗口在最近任务中出现(长按home键)
  • theme="@android:style/Theme.Dialog"来设置对话框主题。

我真的很喜欢你,tbruyelle!添加taskAffinity = ""解决了困扰我很长时间的问题! - Daksh

5

如何在代码中实现launchMode = singleTask的等效功能

我没有看到一个清晰的解释如何在程序中设置这些标志,所以我会在这里包含我的结果。简而言之:你必须设置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_MULTIPLE_TASK。

如果你直接从你的应用程序中启动它,你的对话框将出现在你的应用程序的最后一个Activity上方。但是如果你使用AlarmManager广播的PendingIntent来启动你的“对话框”,你就有时间切换到另一个应用程序,这样你就可以看到你的“对话框”会出现在那个其他应用程序的上方,如果样式被设置为适当地显示其后面的内容。

显然,我们应该在合适的时候负责地显示一个对话框在其他应用程序的上方。

public class MyReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {

// you have to set these flags here where you receive the broadcast
// NOT in the code where you created your pendingIntent
    Intent scheduledIntent = new Intent(context, AlertAlarmActivity.class);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    context.startActivity(scheduledIntent);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接