java - 我怎样才能在android中取消注册接收器?

标签 java android eclipse

我收到了这个错误泄漏了最初在此处注册的 Intent 接收器,但即使出现这种错误,应用程序仍然启动并运行,是什么原因导致该错误?这会影响应用程序吗?

这就是我想做的:

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..

最佳答案

  • 如果您已在 onCreate 中注册了接收器,则在其 onDestroy 方法中取消注册它。

  • 如果您已在 onResume 中注册接收器,请在其 onPause 方法中取消注册。

  • 如果您已在 onStart 中注册接收器,请在其 onStop 方法中取消注册。

关于java - 我怎样才能在android中取消注册接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27298700/

相关文章:

java - 无法启动 Tomcat 8 服务器 Eclipse

java - 如何判断句子的结尾

android - 支持不同的语言

android - 如何获得更准确的位置?

java.lang.ClassNotFoundException : com. google.api.server.spi.SystemServiceServlet

java - 使用 RxJava 生成无限自然数序列

java - WebLogic + Spring 的 JNDI 查找配置错误

c - 为什么这个烦人的波浪号 ~ 会出现在我的 char* 字符串中?

java - 如何在android中的xml中附加标签?以及如何保存该 xml 文件?

android - 如何使用 Eclipse 和 Mercurial 正确克隆 Android 项目