Android短信多文字组

时间:2017-05-24 10:44:03

标签: android sms

我的项目是recyclerView / cardView中的电话号码核对清单。可以通过复选框添加或减少电话号码/商家以创建单个组。我希望能够向选定的个人发送群组多文本。

我的问题是,只有组中的第一个电话号码(收件人)收到该信息,而其余信息没有收到任何信息,但数字仍显示在编辑文本中(第一个是唯一正常运行的号码)。

我尝试了很多不同的方法但没有任何效果,我即将放弃。

似乎没有人知道如何解决这个问题。如果这个问题可以解决,请告诉我。

我不想单独循环数字和文本,这是一个建议的修复。

这是电话活动:

public class ACPhone extends AppCompatActivity {

    private static final String SEPARATOR = ";";
    EditText txtPhoneNo;
    EditText txtMessage;
    TextView txtView;
    Button btnsend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acphone);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);
        txtView = (TextView)findViewById(R.id.txtMessageMass);
        btnsend = (Button) findViewById(R.id.btnSend);

        Intent intent = getIntent();
        if (intent != null){

            ArrayList<CharSequence> selectedNumbers = 
            intent.getCharSequenceArrayListExtra(SELECTED_NUMBERS);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < selectedNumbers.size(); i++) {
                sb.append(selectedNumbers.get(i));
                if (i != selectedNumbers.size() - 1){
                    sb.append(SEPARATOR);
            }
        }
        txtPhoneNo.setText(sb.toString());
    }
    btnsend.setOnClickListener(new View.OnClickListener() {

    @Override public void onClick(View v) {

            String phoneNo = txtPhoneNo.getText().toString();
            String message = txtMessage.getText().toString();
            String messageView = txtView.getText().toString();

            if (phoneNo.length() > 0 && message.length() > 0) {
                sendMessage(phoneNo, message, messageView);
            } else {

                Toast.makeText(getBaseContext(), "Please enter message", 
 Toast.LENGTH_SHORT).show();
            }
        }
    });
    }

    private void sendMessage(String phoneNo,String message, String staticMessage){

        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo,null,message + "\n" + 
   staticMessage,null,null);
            Toast.makeText(getApplicationContext(), "Message Sent", 
            Toast.LENGTH_SHORT).show();

            }
        catch (Exception e){
                Toast.makeText(getApplicationContext(), "Unable to send. Please try again", Toast.LENGTH_SHORT).show();
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您可以创建所有数字的列表,并在onclick或方法中的列表中执行for循环,并在onclick中调用它。无论如何我就是这样做的。

答案 1 :(得分:0)

以下是在检查时将一条消息发送给多个联系人的一些步骤。

第1步:MainActivity.class这样的

public class MainActivity extends AppCompatActivity {

ListView listView;
EditText editMessage;
ProgressDialog progressDialog;
Handler progresshandler;
boolean isThreadRunning;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.contactsView);
    editMessage = (EditText) findViewById(R.id.editMessage);

    listView.setAdapter(new ContactAdapter(this, contacts));

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Sending Messages.. Please wait!");

    progresshandler = new Handler() {
        public void handleMessage(Message msg) {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "Messages Sent",
                    Toast.LENGTH_LONG).show();
        }
    };
  }

}

第2步:在此MainActivity.class中创建一个类(将此类放在onCreate()方法下面)

class SendMessagesThread extends Thread {
    Handler handler;

    public SendMessagesThread(Handler handler) {
        this.handler = handler;
    }

    public void run() {
        SmsManager smsManager = SmsManager.getDefault();
        // Find out which contacts are selected
        for (int i = 0; i < listView.getCount(); i++) {
            View item = (View) listView.getChildAt(i);
            boolean selected = ((CheckBox) item.findViewById(R.id.selected)).isChecked();
            if (selected) {
                String mobile = ((TextView) item.findViewById(R.id.mobile)).getText().toString();
                try {
                    smsManager.sendTextMessage(mobile, null, editMessage.getText().toString(), null, null);
                } catch (Exception ex) {
                    Log.d("Mobile", "Could not send message to " + mobile);
                }
            }
        }
        Message m = handler.obtainMessage();
        handler.sendMessage(m);
    } // run
} // Thread

第3步:创建一个方法(将此方法放在步骤-2下面)

public void sendMessages(View v) {
    if (editMessage.getText().toString().length() > 0) {
        SendMessagesThread thread = new SendMessagesThread(progresshandler);
        thread.start();
        progressDialog.show();
    } else {
        Toast.makeText(this, "Please enter message!", Toast.LENGTH_LONG)
                .show();
    }
}

注意:根据我的项目,我没有使用任何SQLite数据库或webservice.Basically,我从设备联系簿获取所有联系人并将该联系人显示到listview。所以,试着理解和修改。

答案 2 :(得分:0)

public class TextActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayList<CharSequence> selectedNumbers 
 =getIntent().getCharSequenceArrayListExtra(SELECTED_NUMBERS);;

    String phNumbers = "";
    for (CharSequence s: selectedNumbers) {
        phNumbers += s + ";";
    }

 //      for (int i = 0; i < selectedNumbers.size(); i++) {
//            phNumbers += selectedNumbers.get(i);
//          if (i != selectedNumbers.size()-1){
//               phNumbers += ";";
//            }
//        }

    phNumbers = phNumbers.substring(0, phNumbers.length() - 1);
    String message = "";
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", phNumbers);
    smsIntent.putExtra("sms_body",message);
    startActivity(smsIntent);

}
}