BroadcastReceiver中的AlertDialog ?? 可以吗?

Jav*_*iew 9 java android broadcastreceiver android-alertdialog

BroadcastReceiver中的AlertDialog?可以吗?我正在开发一个应用程序,如果我收到短信,会弹出一个对话框.我试图在BroadcaseReceiver中编写代码.但我不能使用这行代码AlertDialog.Builder builder = new AlertDialog.Builder(this);.有人可以帮我提示一下!

public class SMSPopUpReceiver extends BroadcastReceiver {

    private static final String LOG_TAG = "SMSReceiver";
    public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    public void onReceive(Context context, Intent intent) {
        Log.i(LOG_TAG, "onReceive");

        if (intent.getAction().equals(SMSPopUpReceiver.ACTION)) {
            StringBuilder sb = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");

            for (Object pdu : pdus){
                    SmsMessage messages =
            SmsMessage.createFromPdu((byte[]) pdu);

            sb.append("Received SMS\nFrom: ");
            sb.append(messages.getDisplayOriginatingAddress());
            sb.append("\n----Message----\n");
            sb.append( messages.getDisplayMessageBody());
            }
            }
            Log.i(SMSPopUpReceiver.LOG_TAG,
            "[SMSApp] onReceiveIntent: " + sb);
            Toast.makeText
            (context, sb.toString(), Toast.LENGTH_LONG).show();
            }

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.cancel();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });
        AlertDialog alert = builder.create();
    }

}
Run Code Online (Sandbox Code Playgroud)

Zel*_*mir 14

主要问题:尽量避免将耗时的功能放入BroadcastReceiver.它应该只接收并启动绑定的Activity/Service中的进一步处理.

更新:

请查看以下可能有用的来源:

StackOverflow上的类似问题:

如何将数据从BroadcastReceiver发送到android中的Activity?

Android短信接收器不工作

Android SDK演示示例:

Android的SDK窗口\样品\机器人-8\ApiDemos\SRC\COM \示例\机器人\的API\OS\SmsMessagingDemo.java

当然还有标准的Android API文档:http://developer.android.com/reference/android/content/BroadcastReceiver.html

UPDATE2:

添加了应该看的应用程序框架.请注意,未定义任何内容视图.这是因为您的应用程序将具有透明屏幕.实现这一目标

@android:款式/ Theme.Translucent

在AndroidManifest.xml中的此活动的主题标记下输入.

public class NotifySMSReceived extends Activity 
{
    private static final String LOG_TAG = "SMSReceiver";
    public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        IntentFilter filter = new IntentFilter(ACTION);
        this.registerReceiver(mReceivedSMSReceiver, filter);
    }

    private void displayAlert()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?").setCancelable(
                false).setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                }).setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

    private final BroadcastReceiver mReceivedSMSReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (ACTION.equals(action)) 
            {
                //your SMS processing code
                displayAlert();
            }
        }
    };    
}
Run Code Online (Sandbox Code Playgroud)


cch*_*son 7

我一直在研究它,BroadcastReceiver的文档实际上说:

public abstract void onReceive(Context context,Intent intent)

从以下版本开始:API Level 1当BroadcastReceiver接收Intent广播时,将调用此方法.在此期间,您可以使用BroadcastReceiver上的其他方法来查看/修改当前结果值.该函数通常在其进程的主线程内调用,因此您永远不应该在其中执行长时间运行的操作(在考虑阻塞接收器和被杀死的候选者之前,系统允许超时10秒) .您无法在onReceive()的实现中启动弹出对话框.

您无法在onReceive()的实现中启动弹出对话框

所以似乎不可能


Ume*_*esh -1

将 AlertDilaog 中的“this”一词替换为“context”—— onRecieve 方法的第一个参数。

 public void onReceive(Context context, Intent intent)
Run Code Online (Sandbox Code Playgroud)

  • 我是如何得到这个错误的:01-30 16:03:21.748: WARN/WindowManager(34): 尝试使用非应用程序令牌 WindowToken{43c345e8 token=null} 添加窗口。正在流产。 (2认同)