如何从 BroadcastReceiver 类引发警报对话框?

发布于 2024-12-02 01:41:47 字数 1040 浏览 1 评论 0原文

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

BroadcastReceiver 类将使用 AlarmManager 在后台每 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

I have used a timer method in an Activity class. In that method I have an intent from Activity class to a BroadcastReceiver class.

This BroadcastReceiver class will call on every 15 minutes at background by using AlarmManager.

When I call the BroadcastReceiver class I would like to raise an 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();
}

How can I raise an AlertDialog from BroadcastReceiver class from a background process?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-12-09 01:41:47

如果您的活动在 BroadcastReceiver 获取意图时正在运行,您应该能够使用 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 设为内部类,则此方法有效您的活动。

If your activity is running when the BroadcastReceiver gets the intent you should be able to use runOnUiThread to run a method that creates an AlertDialog, e.g.:

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();
        }
    });

}

This works if you make your BroadcastReceiver an inner class to your Activity.

只是在用心讲痛 2024-12-09 01:41:47

简而言之:这是不可能的。

只有活动可以创建/显示对话框。事实上,这个问题已经被问过不止一次了:

另外,这会带来非常糟糕的用户体验:

  • 如果用户不在您的应用程序中(假设他正在播放
    游戏)并且您的对话框每 15 分钟弹出一次,这将非常
    对他来说很烦人。
  • 如果用户在您的应用程序中,还有其他几个(更好的
    适合)通知他某事已被执行的方法。

更适合的方法

事实上,您可以创建/显示 ToastBroadcastReceiver 的 code>。当用户不在“您的应用程序中”时,也会显示此 Toast

此外,您还可以从 BroadcastReceiver 发送通知(显示在屏幕顶部的通知栏中)。 有关如何执行此操作的教程(它没有什么不同与您在活动中执行此操作的方式不同,除了您使用从 onReceive 方法传递的 Context 对象)。

当用户不在“您的应用程序中”时,也会显示通知,IMO 是解决此问题的最佳解决方案。

In short: It is not possible.

Only Activity's can create/show dialogs. In fact, this has been asked more then once:

Also, this would give a very bad user-experience:

  • If the user is not in your application (let's say he's playing a
    Game) and your Dialog pops up every 15 minutes, this will be very
    annoying for him.
  • If the user is in your application, there are several other (better
    suited) ways to notify him that something has been executed.

Better suited ways

In fact, you can create/show a Toast from an BroadcastReceiver. This Toast will also bee shown when the user is not "in your application".

Also, you can send a Notification (shown in the Notification-Bar at the top of your screen) from a BroadcastReceiver. A tutorial on how to do this (it does not differ from how you do it in an Activity, except that you use the passed Context-Object from the onReceive-method).

The Notification will also be shown when the user is not "in your application" and is IMO the best solution to this problem.

秋意浓 2024-12-09 01:41:47

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");

1) In 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) In BroadcastReceiver.onReceive:

YourActivity ac= new YourActivity ();
ac.showAlertDialog(YourActivity.ctx, "test", "test");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文