0

ここで、テキストビューを作成し、そのテキストビューに複数の連絡先を保存してから、このテキストビューの値をインテントを通じて別のアクティビティに渡しました。受信したテキストビューに保存されている複数の連絡先に Sms を送信したい..そして私は以下の次のコードを使用してこれを行いました..しかし、問題は、そのテキストビューに保存されている最初の番号にしか送信できないことです..したがって、保存されているすべての連絡先に送信するための代替コードを提案してください.

これが私のコードです..

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 に答える