如何使用android smsmanager发送多条消息

时间:2016-06-14 07:23:45

标签: java android

我正在尝试开发可以从服务器加载消息和电话号码并在内部存储它的短信应用程序,因此当尝试使用循环一次发送所有消息时,只发送其中一些消息,这是我的代码。请帮忙

   send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            try {

                String SENT = "sent";
                String DELIVERED = "delivered";

                Intent sentIntent = new Intent(SENT);
 /*Create Pending Intents*/
                PendingIntent sentPI = PendingIntent.getBroadcast(
                        getApplicationContext(), 0, sentIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Intent deliveryIntent = new Intent(DELIVERED);

                PendingIntent deliverPI = PendingIntent.getBroadcast(
                        getApplicationContext(), 0, deliveryIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
 /* Register for SMS send action */
                registerReceiver(new BroadcastReceiver() {

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String result = "";

                        switch (getResultCode()) {

                            case Activity.RESULT_OK:
                                result = "Transmission successful";
                                break;
                            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                result = "Transmission failed";

                                break;
                            case SmsManager.RESULT_ERROR_RADIO_OFF:
                                result = "Radio off";
                                break;
                            case SmsManager.RESULT_ERROR_NULL_PDU:
                                result = "No PDU defined";
                                break;
                            case SmsManager.RESULT_ERROR_NO_SERVICE:
                                result = "No service";
                                break;
                        }

                        Toast.makeText(getApplicationContext(), result,
                                Toast.LENGTH_LONG).show();
                    }

                }, new IntentFilter(SENT));
 /* Register for Delivery event */
                registerReceiver(new BroadcastReceiver() {

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        Toast.makeText(getApplicationContext(), "Delivered",
                                Toast.LENGTH_LONG).show();
                    }

                }, new IntentFilter(DELIVERED));

  /*Send SMS*/
                ArrayList<Messages> messagesArrayList = new ArrayList<>();
         /* loading messages and phone into arraylist */
                messagesArrayList = db.getMessages();
                for (int i = 0; i < messagesArrayList.size() ; i++) {

                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage("090" +       messagesArrayList.get(i).getPhone(), null, messagesArrayList.get(i).getBody(), sentPI,
                            deliverPI);



                }

                }catch(Exception ex){
                    Toast.makeText(getApplicationContext(),
                            ex.getMessage().toString(), Toast.LENGTH_LONG)
                            .show();
                    ex.printStackTrace();
                }

        }
    });

1 个答案:

答案 0 :(得分:0)

使用此

String strnum="100864445556;1004522145586;100855822557;100885222559";



Uri smsToUri = Uri.parse("smsto:" + strnum);

如果您从数据库查询中获取数字,那么

借助循环

使字符串strnum = num1 + ";" + num2 + ";" + num3 + ";"如此