java - BroadcastReceiver 中的 AlertDialog??可以吗?

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

}

最佳答案

主要问题:尽量避免将耗时的功能放入 BroadcastReceiver 中。它应该只是在绑定(bind)的 Activity/服务中接收并启动进一步的处理。

更新:

请检查以下可能有用的资源:

StackOverflow 上的类似问题:

How to send data from BroadcastReceiver to an Activity in android?

Android SMS receiver not working

Android SDK 演示示例:

android-sdk-windows\samples\android-8\ApiDemos\src\com\example\android\apis\os\SmsMessagingDemo.java

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

更新 2:

添加了应有的应用框架。请注意,没有定义内容 View 。这是因为您的应用程序将具有透明屏幕。为了实现这一点

@android:style/Theme.Translucent

是在 AndroidManifest.xml 中此 Activity 的主题标签下输入的。

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

关于java - BroadcastReceiver 中的 AlertDialog??可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4844031/

相关文章:

java - 无法调用 Tomcat 管理器 : Server returned HTTP response code: 401

java - 如何检查 Java 类成员顺序

java - Android API 平移/缩放带有 Sprite 覆盖的 ImageView

android - 从广播接收器调用 SetContentView()

java - 短信广播接收器不工作

java - 为什么 AbstractApplicationContext 会为 LogFactory 抛出 NoClassDefFoundError?

java - 如何将 Java 中的 TIFF 图像读入 BufferedImage?

java - map 正在加载,但在 Android 开发中看不到 map

android - 在没有 IDE 的情况下使用 Android 支持库

android - 我的 AlarmDemo 在我的 logcat 中不断崩溃,原因不明