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.