简体   繁体   中英

Android App exits when request Runtime Permissions

In the program below I ask for SMS permission before sending a message.

At first App execution, when the message request is confirmed, the permission are checked and, if missing, a pop up requesting the user to allow the app is issued.

When this pop up is issued, the app is moved in background.

I found many topics about this issue but no fixes for my problem. Eg the nohistory entry is missing in the manifest.

private void checkForSmsPermission() {

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS) !=
            PackageManager.PERMISSION_GRANTED) {
     
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);
        return;

    } else {
        // Permission already granted. Enable the SMS button.
        return;
    }
}



@Override
public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        if (requestCode == MY_PERMISSIONS_REQUEST_SEND_SMS)  {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
            }
        }
    }

public void send_command (String text){

    checkForSmsPermission();


    //Get the SmsManager instance and call the sendTextMessage method to send message
    SmsManager sms=SmsManager.getDefault();
    sms.sendTextMessage(recipient_number, null, text, null,null);


}

This is happening because you are calling following method anyways.

SmsManager sms=SmsManager.getDefault();
 sms.sendTextMessage(recipient_number, null, text, null,null);

You should only call them if you are sure that you have the permission else your app will crash. Its always recommended to call these method on success of permission validation. but in your case it will get call whether the user provided the permission or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM