短信广播接收器接收到多少次? [英] SMS BroadCast Receiver receives many times?

查看:184
本文介绍了短信广播接收器接收到多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序短信广播接收器。当我第一次发送短信弹窗是一个消息的发送短信。当我发出第二次请求弹出消息得到一倍。在第三次它三倍等。我使用以下code发送和接收广播。

I am using sms broadcast receiver in my application. When i send first sms it popups one message as sms sent. when i sent second request the popup message get doubled. In third time it tripled and so on. I am using following code for sending and receiving the broadcast.

  private void sendRequest()
    {        
        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(), "Invalid PhoneNumber", 
                                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));


       SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(sms_phonenumber, null, sms_message, sentPI, null);  

我不知道Ÿ这种情况正在发生。我从mainactivity发送短信。

I am not sure y this is happening. I am sending sms message from mainactivity.

感谢您的帮助家伙..

Thanks for your help guys..

推荐答案

看起来,因为每次调用sendRequest将你注册的BroadcastReceiver一次时间 .......

looks because every time you call sendRequest you register BroadcastReceiver one more time ........

您应该只有一次注册的BroadcastReceiver和生活活动之前应当被注销........

you should register the BroadcastReceiver once only and that should be unregistered before living the activity........

做登记和未登记工作一度只有在onStart和的onStop中的链接

do register and unregistered work once only in onStart and onStop as in link

这篇关于短信广播接收器接收到多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆