Delay several SMS Android Studio [duplicated]

0

Dear community. how could I do so that when sending several SMS there is a difference of X seconds in the shipment. Where X is a different wait time for each message.
For example:

  • Message 1: Immediately when pressing the button
  • Message 2: 20 seconds after message 1
  • Message 3: 10 seconds after message 2
  • Message 4: 5 seconds after message 3
  • Message> = 5: 3 seconds after the previous message.

    Thank you very much for your help.

Here the code.

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
import android.Manifest;
import android.os.Handler;

import java.util.Timer;
import java.util.TimerTask;



public class otrosSMS extends Activity {



    EditText txtPhoneNo;
    EditText txtMessage;
    EditText txttime;
    Button btnSend;
    String p = Manifest.permission.SEND_SMS;

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mensajes);






        txtPhoneNo = (EditText) this.findViewById(R.id.nrotelf);
        txtMessage = (EditText) this.findViewById(R.id.editText2);
        txttime = (EditText) this.findViewById(R.id.editText);

        btnSend = (Button) this.findViewById(R.id.btnsms);



        btnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {


                // Obtener numero de telefono y mensaje

                checkSMSStatePermission();
                String phoneNo = txtPhoneNo.getText().toString();
                String message = "Mensaje de prueba";
                String message1 = "Mensaje de prueba 2";
                String message2= "Mensaje de prueba 3";


                // Si el numero y el sms no esta vacio
                if (phoneNo.length()>0){
                    sendMessage(phoneNo, message);
                    sendMessage(phoneNo, message1);
                    sendMessage(phoneNo, message2);

                    // If phone number or message not empty
                }else{
                    Toast.makeText(getBaseContext(), "Por favor ingrese el numero de telefono del GPS.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    // Funcion Enviar mensaje SMS
    private void sendMessage(final String phoneNo, final String message) {
        try {

            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //enviar sms luego de 20s
                    SmsManager smsManager = SmsManager.getDefault();
                    PendingIntent sentPI;
                    String SENT = "SMS_SENT";
                    //el primer parametro recibe el contexto asi que lo
                    // llamas desde un fragment tendras
                    //cambiar el this por getContext()
                    sentPI = PendingIntent.getBroadcast(otrosSMS.this, 0, new Intent(SENT), 0);
                    smsManager.sendTextMessage(phoneNo, null, message, sentPI, null);
                }
            }, 20000);






            Toast.makeText(getApplicationContext(), "SMS enviado.", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "SMS Fallido. Intente de nuevo!", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }



    private void checkSMSStatePermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(
                this, Manifest.permission.SEND_SMS);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            Log.i("Mensaje", "No se tiene permiso para enviar SMS.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 225);
        } else {
            Log.i("Mensaje", "Se tiene permiso para enviar SMS!");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

}
    
asked by Deivis Barrios 05.07.2017 в 17:27
source

1 answer

0

Without having any idea of this language, but seeing that you already answered what you needed, you just did not know how to interpret it.

To the sendmessage function you would have to add the parameter of the time you want it to wait. To the postDelayed function, instead of passing 20000 by default, you should spend that time that you received as a parameter. And every call to sendmessage, should say the waiting time.

Note that if all this is asynchronous, in reality the delay time of the second is the delay time of the first plus the delay time of the second ...

    
answered by 05.07.2017 в 18:17