how do i repeatedly send a text in android application

526 views Asked by At

I'm playing around with sms messaging following a tutorial and I would like for the message to be sent 4 times in a row when the user hits the send button but I don't know exactly how. I've tried putting for loops in different parts of the code but it is not working.

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SMS extends Activity {

    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;

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

        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);


        btnSendSMS.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();
                if(phoneNo.length()>0 && message.length()>0){
                     for(int i=0; i<5; i++){
                    sendSMS(phoneNo, message);} //this is one of the places where iv'e placed a for loop
                }
                else{
                    Toast.makeText(getBaseContext(), "Please enter both phone number and message.", Toast.LENGTH_SHORT).show();}
            }
        });
    }

    private void sendSMS(String phoneNumber, String message)
    {


        PendingIntent pi = PendingIntent.getActivity(this, 0,new Intent(this, SMS.class), 0);
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, pi, null);

    }
1

There are 1 answers

0
Prejith P On

I don't think this is possible. There are limits as to how many messages can be sent in a specified period of time on Android. That must be why the loops are not working—Android is blocking it. As far as I remember, the limit is 30 messages in 30 minutes.

There are workarounds for this limit, but they are all only accessible from the command shell inside the phone.