0

I'm a beginner in Android. I tried to do application to send SMS. The applications stops working when I try to send a message on my phone. A message appears in the emulator: "Send sent". I don't understand what the problem is. Application code:

public class MainActivity extends AppCompatActivity {

   private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
   Button sendBtn;
   EditText txtphoneNo;
   EditText txtMessage;
   String phoneNo;
   String message;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       sendBtn = (Button) findViewById(R.id.btnSendSMS);
       txtphoneNo = (EditText) findViewById(R.id.editText);
       txtMessage = (EditText) findViewById(R.id.editText2);

       sendBtn.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
               sendSMSMessage();
           }
       });
   }

   protected void sendSMSMessage() {
       phoneNo = "+48" + txtphoneNo.getText().toString();
       message = txtMessage.getText().toString();

       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},
                       MY_PERMISSIONS_REQUEST_SEND_SMS);
           }
       }
   }

   @Override
   public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
       switch (requestCode) {
           case MY_PERMISSIONS_REQUEST_SEND_SMS: {
               if (grantResults.length > 0
                       && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                   SmsManager smsManager = SmsManager.getDefault();
                   smsManager.sendTextMessage(phoneNo, null, message, null, null);
                   Toast.makeText(getApplicationContext(), "SMS sent.",
                           Toast.LENGTH_LONG).show();
               } else {
                   Toast.makeText(getApplicationContext(),
                           "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                   return;
               }
           }
       }

   }
}

In AndroidManifest.xml I wrote:

    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>

Thank you in advance

Leming
  • 29
  • 4
  • What do you mean by "The applications stops working"? Your code is set up to show an "SMS sent" message when you are sending an SMS message. – CommonsWare Nov 21 '20 at 16:36
  • When I press the send button, the application on the phone turns off. – Leming Nov 21 '20 at 17:05
  • Sorry, but I do not know what that means. If you believe that your app is crashing, use Logcat to examine the stack trace associated with your crash: https://stackoverflow.com/q/23353173/115145 – CommonsWare Nov 21 '20 at 17:12

0 Answers0