kaushalyap
kaushalyap

Reputation: 13637

Request permission dialog send app to the background

I have Send SMS button where app ask user for SEND_SMS permission and then send SMS. But when I click Send SMS button app is send to the background and permission request dialog is shown at the home screen. When I click allow and go back to the app I have automatically navigated to the home fragment of the app. Any way if I go back to the fragment with Send SMS button not I can send SMS. (So permission grant is persisted.) My code will look like below

In AndroidManifest.xml I have

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

In MainActivity.kt

 fun requestSendSmsPermission() {

        if (ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                Manifest.permission.SEND_SMS
            )
        ) {
            android.app.AlertDialog.Builder(this)
            .setTitle("Permission Request")
            .setMessage("Reason for permission request")
            .setPositiveButton("ok"
            ) { _, _ ->
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(Manifest.permission.SEND_SMS),
                    SEND_SMS_PERMISSION_CODE
                )
                sendSMS()
            }
            .setNegativeButton("cancel"
            ) { dialog, _ -> dialog.dismiss() }
            .create().show()
        } else {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.SEND_SMS),
                SEND_SMS_PERMISSION_CODE
            )
            sendSMS()
        }

    }

    private fun sendSMS(){
        val emergencyContactNo = "1234567890"
        val messageBody =
            "hello there"
        val smsManager: SmsManager = SmsManager.getDefault()
        smsManager.sendTextMessage(emergencyContactNo, null, messageBody, null, null)
        Toast.makeText(this, "Message Sent", Toast.LENGTH_LONG).show()
    }


  override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        if (requestCode == SEND_SMS_PERMISSION_CODE) {
            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show()
                sendSMS()

            } else {
                Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show()
            }
        }
    }

In SMS sending fragment I have

mainActivity = activity as MainActivity

binding.btnSms.setOnClickListener {
     mainActivity.requestSendSmsPermission()
}

What I tried?

To me it seems issue is with the navigation component and permission dialog combination. So how could I use runtime permission with navigation component?

Upvotes: 0

Views: 666

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21053

The reason is you are getting crash with java.lang.SecurityException: Sending SMS message: does not have android.permission.SEND_SMS .

Because you are calling sendSms synchronously just after requesting permission. You have to wait for permission to be granted or if its already granted the call sendSms right away.

Remove all sendSms call before permission is granted. Checkout the Basic Sample.

fun requestSendSmsPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
        android.app.AlertDialog.Builder(this)
            .setTitle("Permission Request")
            .setMessage("Reason for permission request")
            .setPositiveButton("ok"
            ) { _, _ ->
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(Manifest.permission.SEND_SMS),
                    SEND_SMS_PERMISSION_CODE
                )
            }
            .setNegativeButton("cancel"
            ) { dialog, _ -> dialog.dismiss() }
            .create().show()
    } else {
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.SEND_SMS),
            SEND_SMS_PERMISSION_CODE
        )
    }
}

Upvotes: 1

Related Questions