如何提高从BroadcastReceiver的类警告对话框?对话框、BroadcastReceiver

2023-09-13 01:16:07 作者:不再见

我已经用在活动类的计时器方法。在该方法中我有意向活动类到的BroadcastReceiver 类。

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

的BroadcastReceiver 类将调用后台每隔15分钟用 AlarmManager

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

在我所说的的BroadcastReceiver 类我想提出一个 AlertDialog

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

我如何能提高从后台进程的 AlertDialog 的BroadcastReceiver 类?

推荐答案

在短:是的不可以可以

只有活动的可以创建/显示对话框。事实上,这已被要求超过一次:

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

AlertDialog中的BroadcastReceiver How我可以从一个Android广播接收器显示一个对话框? AlertDialog in BroadcastReceiver How can I display a dialog from an Android broadcast receiver?

另外,为什么要启动一个对话框?

Also, why would you want to launch a Dialog?

如果用户不是在您的应用程序(比方说他玩 游戏)和您的对话框弹出,每15分钟,这将是非常 讨厌他。 如果用户在应用程序中,有几个其他的(更好 某些内容被执行适合)的方式来通知他。 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.

事实上,你可以创建/显示 吐司的BroadcastReceiver 。这吐司也将蜜蜂当用户应用程序中的不显示。

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

此外,您还可以发送从的BroadcastReceiver 的通知(在屏幕顶部显示在通知酒吧)。如何做到这一点一个教程(它不会从不同你如何做一个活动,但您使用通过上下文 -object从的onReceive - 方法)。

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 the (in my eyes) best suited solution to this problem.