如何使用Android Studio中的短信管理器向多个用户发送消息?

时间:2017-06-05 11:43:59

标签: java

我想使用短信管理器向多个用户发送消息。

在我的项目中,有一个表单,其中包含姓名,电话号码,电子邮件ID和邮件字段。在这种情况下,当我点击提交按钮时,邮件应该发送给客户端(填写表单的人)和管理员。

我在客户端和管理员端都收到它。但我只在客户端收到一条重复的邮件。重复消息表示将同一副本发送到管理员端的内容发送到客户端。

以下是我的代码库:

   public class MainActivity extends AppCompatActivity {
String name;
String phone;
String email;
String mess;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ((Button) findViewById(R.id.btnOK)).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String name = ((EditText) findViewById(R.id.name)).getText().toString();
            String phone = ((EditText) findViewById(R.id.phone)).getText().toString();
            String email = ((EditText) findViewById(R.id.email)).getText().toString();
            String mess = ((EditText) findViewById(R.id.txtMessage)).getText().toString();
            String number = "9791083064";
            String receipentsNumber[] = {phone,number};




            Intent mail = new Intent(Intent.ACTION_SENDTO);
            mail.setType("message/rfc822");
            mail.setData(Uri.parse("mailto:sudhanmanoharan@gmail.com"));
            mail.putExtra(Intent.EXTRA_TEXT, "Name: " + name + "\nPhone Number: " + phone + "\nEmail: " + email + "\nMessage: " + mess);
            mail.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(mail);

            try {
                for (int i = 0; i <= receipentsNumber.length; i++) {
                    SmsManager smsManager = SmsManager.getDefault();

                    smsManager.sendTextMessage(receipentsNumber[i], null, "Name: " + name + "\nPhone Number: " + phone + "\nEmail: " + email + "\nMessage: " + mess, null, null);
                }                    //smsManager.sendTextMessage(number, null, "Name: " + name + "\nPhone Number: " + phone + "\nEmail: " + email + "\nMessage: " + mess, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again later!",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
}


}

0 个答案:

没有答案