English 中文(简体)
在用户离开活动后如何接收SMS发送/发送的反馈
原标题:How to receive SMS sent/delivered feedback for individual SMS after user left activity

I have an app that provides SMS messaging. The SMS data is stored in app database. The app supports sending to multiple contacts, so when sending an SMS, I dinamically register a different BroadcastReceiver to listen if each SMS was sent (I use each phone number in the String identifying the action in the IntentFilter).

我在收到发出的确认书后,将SMS写进数据库。

问题是,如果用户在“实体”广播之前就离开活动,那么广播接收器就会丢失,我无法再抓住“实体”,因此数据库没有更新。 我选择的一个工作是,在KeyDown(KeyDown())实施,以防止用户在播放所有斜体之前停止活动,但这一解决办法只使用“Back”纽顿语——“Home”纽顿语的活动是无法捕获的。

我的守则是:

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

《刑法》

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);
    }
问题回答

您可以设立一个服务处(不是信使处),在该处,你可以对所有广播接收者进行即时检查,并采用发送SMS逻辑。 这样,你们的SMS-es就会被送进来,你可以拦截发送/发送的广播。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签