0

在这里,我创建了一个文本视图,并在该文本视图中存储了多个联系人,然后我通过意图将此文本视图值传递给另一个活动。现在我想向存储在接收到的文本视图中的多个联系人发送短信。我已通过使用以下代码完成此操作..但问题是我只能发送到存储在该 textview 中的第一个号码..所以请建议一些替代代码将其发送给所有存储的联系人。

这是我的代码..

Bundle bundle=intent2.getExtras();
final String getudisp=bundle.getString("InvisibleNum");

String number =getudisp;
sendSMS(number, message);

private void sendSMS(String number, String message) {

    Intent sentIntent = new Intent(INTENT_ACTION_SENT);
    PendingIntent pendingSentIntent = PendingIntent.getBroadcast(this,
            REQUEST_CODE_ACTION_SENT, sentIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent deliveryIntent = new Intent(INTENT_ACTION_DELIVERY);
    PendingIntent pendingDeliveryIntent = PendingIntent.getBroadcast(this,
            REQUEST_CODE_ACTION_DELIVERY, deliveryIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    SmsManager smsManager = SmsManager.getDefault();

    // Second parameter is the service center number. Use null if you want
    // to use the default number
    smsManager.sendTextMessage(number, null, message, pendingSentIntent,
            pendingDeliveryIntent);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(smsSentDeliveredReceiver);
}

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter(INTENT_ACTION_SENT);
    filter.addAction(INTENT_ACTION_DELIVERY);

    registerReceiver(smsSentDeliveredReceiver, filter);
}

private void initializeReceivers() {
    smsSentDeliveredReceiver = new BroadcastReceiver() {

        public void onReceive1(Context context, Intent intent) {
            processBroadcasts(intent);
        }

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub

        }
    };
}

private void processBroadcasts(Intent intent) {
    String action = intent.getAction();
    Log.i(TAG, "Received: " + action);

    if (action.equals(INTENT_ACTION_SENT)) {
        Bundle bundle = intent.getExtras();
        // Need to check for error messages
        Log.i(TAG, "Message: Sent");
        Toast.makeText(this, "Message sent", Toast.LENGTH_LONG).show();
    } else if (action.equals(INTENT_ACTION_DELIVERY)) {
        Bundle bundle = intent.getExtras();
        Set<String> keys = bundle.keySet();
        // Need to check for error messages
        Log.i(TAG, "Message: Delivered");
        Toast.makeText(this, "Message delivered", Toast.LENGTH_LONG).show();
    }
}
4

1 回答 1

2

以这种方式创建一个类MultipleSMS


  private void MultipleSMS(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:
                    ContentValues values = new ContentValues();
                    for (int i = 0; i < MobNumber.size() - 1; i++) {
                        values.put("address", MobNumber.get(i).toString());  
                        // txtPhoneNo.getText().toString());
                        values.put("body", MessageText.getText().toString());
                    }
                    getContentResolver().insert(
                            Uri.parse("content://sms/sent"), values);
                    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);
    }  

现在以这种方式调用MultipleSMS


这里MobNumber是一个包含phoneNumbers的数组

        for (int i = 0; i < MobNumber.size(); i++) 
        {
                String message = MessageText.getText().toString();
                String tempMobileNumber = MobNumber.get(i).toString();
                MultipleSMS(tempMobileNumber, message);
         }
于 2012-08-07T06:14:31.810 回答