获得多个节目的意图? [英] Getting multiple broadcasts from intents?

查看:86
本文介绍了获得多个节目的意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://mobiforge.com/developing/story/sms-messaging-android

我使用的例子code在上面的链接我自己的应用程序发送短信,但我遇到一个问题,检查我的邮件的发送状态的时候。会发生什么事是,敬酒消息将弹出的每一个消息,我已经尝试发送。所以基本上,我们说,我已经发3消息。当我去送我的第四消息,敬酒的消息会弹出4次。看来,也许是的BroadcastReceiver从目前使用的每一个意图接收相同的广播?我无法弄清楚到底为什么发生这种情况,或如何阻止它。任何帮助或洞察力将大大AP preciated!

下面是导致这种具体方法:

  // ---发送短信息到另一台设备---
私人无效sendSMS(字符串phoneNumber的,字符串消息)
{
    字符串SENT =SMS_SENT;
    字符串DELIVERED =SMS_DELIVERED;

    PendingIntent sentPI = PendingIntent.getBroadcast(此,0,
        新的意图(SENT),0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
        新的意图(交付),0);

    // ---当短信已发送---
    registerReceiver(新BroadcastReceiver的(){
        @覆盖
        公共无效的onReceive(背景为arg0,意图ARG1){
            开关(的getResult code())
            {
                案例Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(),短信发送
                            Toast.LENGTH_SHORT).show();
                    打破;
                案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(),一般故障,
                            Toast.LENGTH_SHORT).show();
                    打破;
                案例SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(),无服务,
                            Toast.LENGTH_SHORT).show();
                    打破;
                案例SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(),空的PDU,
                            Toast.LENGTH_SHORT).show();
                    打破;
                案例SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(),无线电关
                            Toast.LENGTH_SHORT).show();
                    打破;
            }
        }
    },新的IntentFilter(发送));

    // ---当SMS已交付---
    registerReceiver(新BroadcastReceiver的(){
        @覆盖
        公共无效的onReceive(背景为arg0,意图ARG1){
            开关(的getResult code())
            {
                案例Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(),SMS递送,
                            Toast.LENGTH_SHORT).show();
                    打破;
                案例Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(),短信未交付,
                            Toast.LENGTH_SHORT).show();
                    打破;
            }
        }
    },新的IntentFilter(交付));

    SmsManager的短信= SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
}
 

解决方案

首先,你有一个新的BroadcastReceiver(){每次调用时间 sendSMS ,所以他们每次调用 sendSMS 时间堆积起来,多了一个。你可以添加取消注册(这一点); 在每个底部的BroadcastReceiver ,但更好的是将移动广播接收器出的这种功能。您可以创建+注册之一 onResume()注销的onPause()。

二,阅读<一个href="http://www.gauntface.co.uk/pages/blog/2010/01/04/proximity-alerts-in-android/">link

如果你想与发送数据的 pendingIntent 你需要帮助的Andr​​oid区分待处理的意图更好...
例如

  PendingIntent deliveredPI = PendingIntent.getBroadcast(这一点,uniqueIdPerSMS ++,
    新的意图(交付),PendingIntent.FLAG_CANCEL_CURRENT);
 

http://mobiforge.com/developing/story/sms-messaging-android

I used the example code in the above link my own application for sending an SMS, but I run into a problem when checking the sent status of my message. What happens is, the toast message will pop up for every message I have attempted to send. So basically, let's say I've already sent 3 messages. When I go to send my 4th message, the toast message will pop up 4 times. It seems that perhaps the BroadcastReceiver is receiving the same broadcast from every intent used so far? I cannot figure out exactly why this is happening, or how to stop it. Any help or insights will be greatly appreciated!

Here is the specific method that causes this:

 //---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{        
    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);        
}

解决方案

First, you have a new BroadcastReceiver(){ every time you call sendSMS, so they pile up, one more each time you call sendSMS. You could add unregister(this); at the bottom of each BroadcastReceiver but better would be to move the Broadcast receiver out of this function. You can create+register one in onResume() and unregister it in onPause().

Second, read this link

if you ever want to send data along with your pendingIntent you need to help Android distinguish your pending intents better ...
e.g.

 PendingIntent deliveredPI = PendingIntent.getBroadcast(this, uniqueIdPerSMS++,
    new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);

这篇关于获得多个节目的意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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