Delay sending SMS

0

I would like to know how I do to delay 20 seconds in sending the next SMS. I want several to be sent with a difference of 20 seconds each.

This is my code:

import android.app.Activity;
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 java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;


public class Mensajes 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";

                // If phone number & message is not empty
                if (phoneNo.length()>0){
                    sendMessage(phoneNo, message);
                    // If phone number or message not empty
                }else{
                    Toast.makeText(getBaseContext(), "Por favor ingrese el numero de telefono del GPS.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    // Function send message sms
    private void sendMessage(String phoneNo, String message){
        try {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, message, null, null);
            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 04.07.2017 в 17:46
source

1 answer

3

Changing your sendMessage function a bit this would help:

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

        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(this, 0, new Intent(SENT), 0);
            smsManager.sendTextMessage(phoneNo, null, message, sentPI, null);
        }
    }, 20000);
}

This worked for me to send text msgs and the postDelayed asks for the time before executing in milliseconds.

I hope I helped you

    
answered by 04.07.2017 в 22:20