用户离开活动后如何接收个人短信的短信发送/传送反馈

时间:2012-05-03 11:57:08

标签: android sms broadcastreceiver send feedback

我有一个提供短信的短信。 SMS数据存储在app数据库中。 该应用程序支持发送到多个联系人,因此在发送短信时,我会动态注册一个不同的BroadcastReceiver来监听每个短信是否被发送(我使用字符串中的每个电话号码来识别IntentFilter中的操作)。

我收到发送的确认后,在数据库中写短信。

问题是,如果用户在“已发送”的Intent广播之前离开活动,则BroadcastReceivers将丢失,我无法再捕获“已发送”的Intent,因此数据库不会更新。我选择的一个解决方法是实现onKeyDown()以防止用户在广播所有Intent之前关闭活动,但此解决方案仅适用于“后退”按钮 - 无法捕获“主页”按钮事件。

这是我的代码:

public void sendSMS(String[] phoneNumbers, String message){   

  final String currentMessage = message;
  SmsManager sms = SmsManager.getDefault(); 
  ArrayList<String> parts = sms.divideMessage(message);

     for(int i=0; i<phoneNumbers.length; i++){

    for(int j=0; j<parts.size(); j++){

        BroadcastReceiver sent = new BroadcastReceiver(){

             public void onReceive(Context arg0, Intent arg1) {             

                  String[] arg = arg1.getAction().split(KEY_SMS_SENT);  
                  String phoneNo = Utils.setSimpleFormatNumber(arg[1]);
                  String count = arg[0];
                  String parts = count.split(KEY_SMS_PART_NO)[0];
                  String partNo = count.split(KEY_SMS_PART_NO)[1];

                      sentReceivers.remove(this);
                      unregisterReceiver(this); 

                        switch (getResultCode())
                        {
                            case Activity.RESULT_OK:                    
                                if(parts.equals(partNo)){
                                    Toast.makeText(getBaseContext(), context.getString(R.string.sms_sent_message), 
                                        Toast.LENGTH_SHORT).show();                                                                                     

                           dbAdapter.createSentSMS(KEY_SMS_TYPE_SENT, phoneNo, currentMessage, Utils.getTimeStamp());                                                                       
                                }
                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                if(parts.equals(partNo)){
                                    Toast.makeText(getBaseContext(), context.getString(R.string.sms_generic_failure_message), 
                                            Toast.LENGTH_SHORT).show();                                                                             

                                    dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());                                                                        
                                }
                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:                                
                                if(parts.equals(partNo)){
                                    Toast.makeText(getBaseContext(), context.getString(R.string.sms_no_service_message), 
                                            Toast.LENGTH_SHORT).show();                                                                             

                                    dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());                                                                        
                                }
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:                              
                                if(parts.equals(partNo)){
                                    Toast.makeText(getBaseContext(), context.getString(R.string.sms_null_pdu_message), 
                                            Toast.LENGTH_SHORT).show();                                                                             

                                    dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());                                                                        
                                }
                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:                            
                                if(parts.equals(partNo)){
                                    Toast.makeText(getBaseContext(), context.getString(R.string.sms_radio_off_message), 
                                            Toast.LENGTH_SHORT).show();                                                                         

                                    dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());                                                                        
                                }
                                break;
                        }
                    }
                };


                registerReceiver(sent, new IntentFilter(String.valueOf(parts.size()-1) + KEY_SMS_PART_NO + String.valueOf(j) + KEY_SMS_SENT  + phoneNumbers[i]));

                //I add the BroadcastReceiver-s to a Vector in order to keep track of them
                sentReceivers.add(sent);

                }
            }
            //I use an IntentService to do the actual sending
            Intent intent = new Intent(context, SMSSendService.class);
            intent.putExtra(KEY_SELECTED_PHONE_NUMBERS, phoneNumbers);
            intent.putExtra(KEY_SMS_MESSAGE, message);
            intent.putExtra(KEY_ACTION, KEY_REQUEST_SEND_SMS);
            startService(intent);
        }

这是IntentService中的代码:

public void sendSMS(String phoneNo, String message, SmsManager sms)
    {           
        ArrayList<String> parts = sms.divideMessage(message); 

        ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>();

        for(int i=0; i<parts.size(); i++){
            sentPIs.add(PendingIntent.getBroadcast(context, 0, new Intent(String.valueOf(parts.size()-1) + KEY_SMS_PART_NO + String.valueOf(i) + KEY_SMS_SENT + phoneNo), 0));

        }

        sms.sendMultipartTextMessage(phoneNo, null, parts, sentPIs, deliveredPIs);
    }

1 个答案:

答案 0 :(得分:0)

您可以创建一个服务(而不是IntentService),您可以在其中实例化所有BroadcastReceivers并执行发送SMS逻辑。这样,您的短信将在后台发送,您可以拦截已发送/已发送的广播。