如何从BroadcastReceiver类中触发警示对话框?

4

我在一个Activity类中使用了计时器方法。在该方法中,我有一个从Activity类到BroadcastReceiver类的意图。

通过使用AlarmManager,这个BroadcastReceiver类将在后台每15分钟调用一次。

当我调用BroadcastReceiver类时,我想弹出一个AlertDialog

public void timerMethod(){
    Intent intent = new Intent(Activity.this,
      BroadcastReceiverClass.class
    );

    PendingIntent sender = PendingIntent.getBroadcast(
      QualityCallActivity.this,0, intent, 0
    );

    // We want the alarm to go off 30 seconds from now.
    long firstTime = SystemClock.elapsedRealtime();

    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    firstTime, 60*1000, sender);
}

BroadcastReceiverClass.java

public void onReceive(Context context, Intent intent)
{
    dialogMethod();
}

我该如何从后台进程中的BroadcastReceiver类中引发一个AlertDialog
3个回答

7

如果您的活动正在运行,当广播接收器接收到意图时,您应该能够使用runOnUiThread来运行一个创建AlertDialog的方法,例如:

public void onReceive(Context context, Intent intent)
{
    runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder d = new AlertDialog.Builder(MyActivity.this);
            b.setMessage("This is a dialog from within a BroadcastReceiver");
            b.create().show();
        }
    });

}

如果你把BroadcastReceiver作为Activity的内部类,那么就可以这样做。


6
简而言之:这是不可能的。
只有Activity可以创建/显示对话框。实际上,这已经被问过多次:
- AlertDialog in BroadcastReceiver - How can I display a dialog from an Android broadcast receiver?
此外,这会给用户带来非常糟糕的体验:
- 如果用户不在您的应用程序中(比方说他正在玩游戏),并且您的对话框每15分钟弹出一次,这将非常令人烦恼。 - 如果用户在您的应用程序中,有几种其他(更合适)的方法来通知他执行了某些操作。
更适合的方法
实际上,您可以从BroadcastReceiver中创建/显示Toast。当用户不在“您的应用程序”中时,也会显示此Toast。
此外,你可以从BroadcastReceiver发送通知(在屏幕顶部的通知栏中显示)。如何执行此操作的教程(与在Activity中执行的方式没有区别,只需使用来自onReceive方法的传递的Context对象即可)。当用户不在“你的应用程序”中时,通知也会显示出来,这是解决此问题的最佳方案。

1
虽然你对于是否使用 AlertDialog 的观点很有道理,但是从 BroadcastReceiver 中显示它是完全可行的。正如我在我的回答中指出的那样,如果你的 BroadcastReceiver 是你的活动的内部类,它就可以工作。请查看我的更新后的答案以获取代码。 - Joel F
但是你的解决方案仅在 BroadcastReceiver 是 Activity 的内部类,并且仅当接收器被“调用”时该 Activity 被打开时才有效。这感觉有点 hackie,似乎不是 Android 开发人员所想要的。相反,使用 ToastNotification 在没有这些限制的情况下可以很好地工作。 - Lukas Knuth
1
就像你所说的,在许多情况下这可能不是最佳选择。但这并不意味着永远不会有一种情况它是正确的选择,因此了解所有选项是有意义的。所以我只是想明确一下,事实上是可能的,因为那是他的问题。 - Joel F

-1

1)在Activity中:

public static Context ctx;

onCreate {
    ctx = this;
}

public void showAlertDialog(Context context, String title, String message) {

    final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting OK Button
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Okay",
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) 
        {
            alertDialog.dismiss();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

2) 在 BroadcastReceiver.onReceive 中:

YourActivity ac= new YourActivity ();
ac.showAlertDialog(YourActivity.ctx, "test", "test");

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