如何从 BroadcastReceiver 设置 Alertbox [英] How to setup Alertbox from BroadcastReceiver

查看:33
本文介绍了如何从 BroadcastReceiver 设置 Alertbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在安卓应用中实现了闹钟.警报工作正常.Toast 消息可见.现在我想向用户发出警报框通知.

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 类的代码.我试过

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.

您有一个替代解决方案来显示来自 Receiver 的 AlertDialog 等活动.这是可能的.

You have an alternate solution to show an Activity like AlertDialog from Receiver. This is possible.

要将活动作为对话框启动,您应该将清单中的活动主题设置为 <activity android:theme="@android:style/Theme.Dialog"/>

To start Activity as dialog you should set theme of activity in manifest as <activity android:theme="@android:style/Theme.Dialog" />

将任何活动设置为Android 中的警报对话框

要从 Receiver 启动 Activity,请使用类似的代码

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(Context, Intent).一旦您的代码从此返回功能,系统认为该对象已完成,不再活跃.

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(Context, Intent) 实现:任何需要异步操作不可用,因为您需要从函数返回处理异步操作,但在这一点 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 中.对于前者,您应该改为使用通知管理器 API.对于后者,您可以使用Context.startService() 向服务发送命令.更多...

所以更好的方法是显示通知",替代方法是将活动用作警报.."

So the better way is 'show notification' and alternate way is 'to use Activity as an Alert..'

快乐编码 :)

这篇关于如何从 BroadcastReceiver 设置 Alertbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆