0

I want to add random delay between 16 to 30 seconds in sending sms in sendSMS Function, I want to know on how to integrate delay for this function and where is the right position to add the seconds delay.

Here is Code:

   private void sendSMSorUSSD(String to, String message, int simNumber, String messageId) {
        if (simNumber > 2) simNumber = 2;
        if (to.startsWith("*")) {
            if (to.trim().endsWith("#")) {
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    Fungsi.log("CALL_PHONE not granted");
                    return;
                }
                Fungsi.log("USSD to " + to + " sim " + simNumber);
                writeLog(messageId + " QUEUE USSD: " + to + " SIM " + simNumber, this);

                queueUssd(to,(simNumber == 0) ? 1 : simNumber);
            } else {
                Fungsi.log("not end with #");
                writeLog("USSD not end with # : " + to, this);

            }
        } else {
            Fungsi.log("send SMS " + to);
            writeLog(messageId + " SEND SMS: " + to + " SIM " + simNumber + "\n" + message, this);

            if (simNumber > 0) {
                SmsManager smsManager = SmsManager.getDefault();
                ArrayList<String> messageList = smsManager.divideMessage(message);
                boolean sukses = true;
                if (messageList.size() > 1) {
                // Is this the right place to put the postDelayed?
                    sukses = SimUtil.sendMultipartTextSMS(this, simNumber - 1, to, null, messageList);
                } else {
                    sukses = SimUtil.sendSMS(this, simNumber - 1, to, null, message, 0);
                }
            } else {
                Fungsi.sendSMS(to, message, this);
            }
        }
    }
Unswaa20
  • 5
  • 4

1 Answers1

0

You can update your method sendSMS() and put it's content inside the following postDelayed function:

val delay = (Random.nextInt(16, 30) * 1000).toLong()

Handler(Looper.getMainLooper()).postDelayed({
    // run you method here
}, delay)

So, no matter where to call your method, it will always work with X delayed seconds.

Hope this help!

Ernis A.
  • 141
  • 1
  • 3
  • it's has error would mind if you put in my sendSMSorUSSD() function that i stated above. I'm sorry becuase i'm new to java – Unswaa20 Jul 07 '22 at 01:50