广播接收器有时会崩溃android

时间:2012-12-23 11:00:19

标签: android broadcastreceiver

我正在使用此代码发送短信。

private void sendSMS(String phoneNumber, String message) {

        System.out.println("SMS: " + message);
        System.out.println("to " + phoneNumber);

        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);
        // ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, new IntentFilter(SENT));

        // ---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        }, new IntentFilter(DELIVERED));

//      SmsManager sms = SmsManager.getDefault();
//      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);


        SmsManager sm = SmsManager.getDefault();
        ArrayList<String> parts = sm.divideMessage(message);
        int numParts = parts.size();

        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

        for (int i = 0; i < numParts; i++) {
            sentIntents.add(sentPI);
            deliveryIntents.add(deliveredPI);
        }

        sm.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
                deliveryIntents);
    }
}

有时我会收到此错误,而其他时候它会正常播放并且完全出错:

android.app.IntentReceiverLeaked: Activity com.example.senddemo.MainActivity has leaked IntentReceiver com.example.senddemo.MainActivity$6@40548d50 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:836)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:823)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:817)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
12-23 15:23:32.800 E/ActivityThread(27024):     at com.example.senddemo.MainActivity.sendSMS(MainActivity.java:331)
12-23 15:23:32.800 E/ActivityThread(27024):     at com.example.senddemo.MainActivity.access$0(MainActivity.java:290)
12-23 15:23:32.800 E/ActivityThread(27024):     at com.example.senddemo.MainActivity$2.onClick(MainActivity.java:120)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.view.View.performClick(View.java:2552)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.view.View$PerformClick.run(View.java:9229)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.os.Handler.handleCallback(Handler.java:587)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.os.Looper.loop(Looper.java:138)
12-23 15:23:32.800 E/ActivityThread(27024):     at android.app.ActivityThread.main(ActivityThread.java:3701)
12-23 15:23:32.800 E/ActivityThread(27024):     at java.lang.reflect.Method.invokeNative(Native Method)
12-23 15:23:32.800 E/ActivityThread(27024):     at java.lang.reflect.Method.invoke(Method.java:507)
12-23 15:23:32.800 E/ActivityThread(27024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-23 15:23:32.800 E/ActivityThread(27024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
12-23 15:23:32.800 E/ActivityThread(27024):     at dalvik.system.NativeStart.main(Native Method)
12-23 15:23:32.810 E/ActivityThread(27024): Activity com.example.senddemo.MainActivity has leaked IntentReceiver com.example.senddemo.MainActivity$5@405486e0 that was originally registered here. Are you missing a call to unregisterReceiver()?

我是否需要在xml文件或OnDestry函数中添加任何内容?因为我不喜欢广播接收器。为什么有时会播放,有些则不播放?

1 个答案:

答案 0 :(得分:1)

您必须保存广播接收器的参考,并取消注册接收器。

像这样更改你的代码:

private BroadcastReceiver mSentReceiver;
private BroadcastReceiver mDeleiveryReceiver;



private void sendSMS(String phoneNumber, String message) {

        System.out.println("SMS: " + message);
        System.out.println("to " + phoneNumber);

        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);
        // ---when the SMS has been sent---

mSentReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        });

        registerReceiver(mSentReceiver, new IntentFilter(SENT));

        // ---when the SMS has been delivered---

mDeleiveryReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
            }
        });
        registerReceiver(mDeleiveryReceiver, new IntentFilter(DELIVERED));

//      SmsManager sms = SmsManager.getDefault();
//      sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);


        SmsManager sm = SmsManager.getDefault();
        ArrayList<String> parts = sm.divideMessage(message);
        int numParts = parts.size();

        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();

        for (int i = 0; i < numParts; i++) {
            sentIntents.add(sentPI);
            deliveryIntents.add(deliveredPI);
        }

        sm.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
                deliveryIntents);
    }
}


@onDestroy(){
if(mSentReceiver != null) unregisterReceiver(mSentReceiver);
if(mDeleiveryReceiver != null) unregisterReceiver(mDeleiveryReceiver);
}