如何从广播接收器设置Alertbox [英] How to setup Alertbox from BroadcastReceiver
问题描述
我在Android应用程序来实现报警。报警工作正常。 吐司
消息是可见的。
现在我想让警报框通知用户
I have implemented alarm in android app. Alarm is working fine. Toast
message is visible.
Now I want to make Alert Box Notification to user.
下面是 ReceiverActivity
类code。我试过
Here is code from ReceiverActivity
Class. which I tried
public class ReceiverActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Code....
new AlertDialog.Builder(context)
.setTitle("Alert Box")
.setMessage("Msg for User")
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
// some coding...
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
}).create().show();
}
}
推荐答案
的虽然不能从接收机显示AlertDialog,因为它需要ActivityContext。的
您有一个替代的解决方案,以显示像AlertDialog从接收器的活动。这是可能的。
You have an alternate solution to show an Activity like AlertDialog from Receiver. This is possible.
要开始活动的对话框中,你应该设置在明显的活动主题为<活动安卓主题=@安卓风格/ Theme.Dialog/>
To start Activity as dialog you should set theme of activity in manifest as <activity android:theme="@android:style/Theme.Dialog" />
要从接收器使用code开始活动,比如
To start Activity from Receiver use code like
//Intent mIntent = new Intent();
//mIntent.setClassName("com.test", "com.test.YourActivity");
Intent mIntent = new Intent(context,YourActivity.class) //Same as above two lines
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);
和不使用AlertDialog从接收器(即使你设法显示AlertDialog)是
And one more reason behind not using AlertDialog from receiver (Even if you managed to show AlertDialog) is
一个BroadcastReceiver对象仅适用于呼叫的持续时间
到的onReceive(上下文,意图)。一旦从这个您code回报
功能,系统认为对象被完成,并且不再
活动的。
A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.
这有你能在做什么重要的影响
的onReceive(上下文,意图)执行:任何需要
异步操作不可用,因为你需要
从函数返回处理异步操作,但在
该点的BroadcastReceiver不再有效并且因此
系统是免费的异步操作之前杀死它的进程
完成。
This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.
在特别的的你可能不会显示一个对话框或绑定到从服务
一个BroadcastReceiver在的。对于前者,你应该使用的
NotificationManager API。对于后者,你可以使用
Context.startService()发送一个命令到服务。 更多...
所以,更好的方法是节目的通知和另一种方法是用活动作为警报..
So the better way is 'show notification' and alternate way is 'to use Activity as an Alert..'
快乐编码:)的
Happy coding :)
这篇关于如何从广播接收器设置Alertbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!