首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android中注销receiver?

如何在android中注销receiver?
EN

Stack Overflow用户
提问于 2014-12-05 00:12:46
回答 3查看 510关注 0票数 0

我得到了这个错误的东西Leaked Intent Receiver that was originally registered here,但应用程序仍然启动和运行,即使有这个错误的东西,是什么导致了这个错误?这对应用程序有影响吗?

这就是我想要做的:

代码语言:javascript
复制
private void sendSMS(String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
    try {
        SmsManager sms = SmsManager.getDefault();
        ArrayList<String> mSMSMessage = sms.divideMessage(message);
        for (int i = 0; i < mSMSMessage.size(); i++) {
            sentPendingIntents.add(i, sentPI);
            deliveredPendingIntents.add(i, deliveredPI);
        }
        sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
                sentPendingIntents, deliveredPendingIntents);

    } catch (Exception e) {

        e.printStackTrace();
        Toast.makeText(this, "SMS sending failed...", Toast.LENGTH_SHORT)
                .show();
    }

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

预付tnx ..

EN

回答 3

Stack Overflow用户

发布于 2015-05-26 03:49:52

  • 如果您已在onCreate中注册接收器,则在其onDestroy方法中注销它。
  • 如果您已在onResume中注册接收器,则在其onPause方法中注销它。
  • 如果您已在onStart中注册接收器,则在其onStop方法中注销它。
票数 1
EN

Stack Overflow用户

发布于 2014-12-05 00:27:15

这个问题很可能是因为您从中调用sendSMSActivity已经退出,但这是您将接收器注册为匿名内部类的上下文。您需要取出这些接收器,或者保留对它们的引用并在您的onPause()中注销它们。

票数 0
EN

Stack Overflow用户

发布于 2014-12-05 00:33:14

由于接收方是在活动内部注册的,而不是在清单中注册的,因此需要取消注册onPause或onStop

代码语言:javascript
复制
 public void onStop() {
        super.onStop();
        if (myReceiver != null) {
            unregisterReceiver(myReceiver);
        }

我忘记提到应该在内部类中创建BroadcastReceiver,比如

代码语言:javascript
复制
  private class myReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //Add your code here
        }
    }

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27298700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档