如何从 BroadcastReceiver 类引发警报对话框?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的活动在 BroadcastReceiver 获取意图时正在运行,您应该能够使用 runOnUiThread 来运行创建 AlertDialog 的方法,例如:
如果您将 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.:This works if you make your BroadcastReceiver an inner class to your Activity.
简而言之:这是不可能的。
只有活动可以创建/显示对话框。事实上,这个问题已经被问过不止一次了:
另外,这会带来非常糟糕的用户体验:
游戏)并且您的对话框每 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:
Game) and your Dialog pops up every 15 minutes, this will be very
annoying for him.
suited) ways to notify him that something has been executed.
Better suited ways
In fact, you can create/show a
Toast
from anBroadcastReceiver
. ThisToast
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 passedContext
-Object from theonReceive
-method).The Notification will also be shown when the user is not "in your application" and is IMO the best solution to this problem.
1) 在 Activity 中:
2) 在
BroadcastReceiver.onReceive
中:1) In Activity:
2) In
BroadcastReceiver.onReceive
: