1

I have a simple app to send alert SMS to my volunteer organization with the click of a button but SMS are not always sent, even though the toast says that the function was executed successfully.

I have tried installing and uninstalling the app multiple times but still the same issue. Also, as you may see I have added the call, that actually sends the SMS, to multiple different places, but I am positive that this is not the issue, as the successful toast is shown, meaning that the app passes through that point.

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends Activity {
    private ArrayList<String> phones = new ArrayList<String>();
    private static final int PERMISSION_REQUEST_SEND_SMS = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button emergency_btn = findViewById(R.id.emergency_btn);
        fill_numbers();
        emergency_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("tag", "onClick: clicked");
                SMS_handler();
            }
        });
    }

    private void SMS_handler(){

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)!=
                PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.SEND_SMS)){
            }
            else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS},
                        PERMISSION_REQUEST_SEND_SMS);
            }
        }
        else {
            send_SMS();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_SEND_SMS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    send_SMS();
                }
                else {
                    Toast.makeText(getApplicationContext(), "SMS failed, please try again", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    private void send_SMS() {
        SmsManager smsManager = SmsManager.getDefault();

        for (int i = 0; i < phones.size(); i++){
            smsManager.sendTextMessage(phones.get(i), null,
                    "ΠΥΡΑΣΦΑΛΕΙΑ ΒΑΡΗΣ\nΕΠΕΙΓΟΝ\nΜΕΤΑΒΕΙΤΕ ΑΜΕΣΑ ΣΤΟΝ ΣΤΑΘΜΟ\n\n" +
                    "PYRASFALEIA VARIS\nEMERGENCY\nPLEASE PROCEED TO THE STATION IMMEDIATELY", null, null);
            System.out.println("sending ");
            System.out.println(i);
        }
        Toast.makeText(getApplicationContext(), "SMS Sent", Toast.LENGTH_LONG).show();
    }


    private void fill_numbers() {
        phones.add("xxxxx");
        phones.add("xxxxxx");

    }
}

Do you think that maybe it is a phone issue and not an app issue? If I try to send an SMS manually it works fine.

Svestis
  • 342
  • 4
  • 12
  • 1
    For one thing, your message might be too long for `sendTextMessage()`, and that method usually just fails silently when that happens. You'd need to use `sendMultipartTextMessage()` instead, splitting the text up with `divideMessage()` first. That alone might get your example working, but you really shouldn't be using an unthrottled loop to send, 'cause if the system can't keep up, it'll start dropping sends and giving generic failure errors, but you're not set up to get the errors, so you're not seeing them. [My answer here](https://stackoverflow.com/a/24845193) has some pointers. – Mike M. Aug 13 '21 at 17:33
  • Hi @MikeM. , I think that this was the issue. Managed to fix it this morning before seeing your reply by reducing the message length (and removing some redundant elements). If you wish please post this as an answer so that I can close this, otherwise, I can also answer my own question by referencing your reply. Up to you, thanks for the help! – Svestis Aug 14 '21 at 10:26
  • I'm good. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Aug 14 '21 at 15:27

1 Answers1

0

As suggested by @Mike.M in the comments, the text message was just too big. Breaking down the message in different SMS or truncating the message worked.

The answer here can also help further.

Svestis
  • 342
  • 4
  • 12