Authorization Dialog Box Does Not Appear on Android 6.0.1 (Marshmallow)

advertisements
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
        try {
            Cursor temp = db.rawQuery("Select name from profile;", null);
            temp.moveToFirst();
            Toast.makeText(TrustedContacts.this, ""+c.getString(2), Toast.LENGTH_SHORT).show();
            (SmsManager.getDefault()).sendTextMessage("0"+c.getString(2), null, "Automated SMS by Medical Alert App by " + temp.getString(0), null, null);
            Toast.makeText(TrustedContacts.this, "SMS Sent!", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
            Toast.makeText(TrustedContacts.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    } else
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);

Here is the Code for sending SMS through SMS Manager on Permission Granted, but the Permission Dialog Box is not showing up.

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

I Have added the Permission in Manifest Files too. Same Scenario for Receive Boot Complete Permission. However, the Call_Phone Permission works Perfectly. Cannot identify the issue. Android Studio TargetAPI 23 and MinAPI is 15.


If you have taken some DANGEROUS PERMISSION in your application which is crucial for user’s perspective that may cause privacy issue like (SEND SMS, READ CONTACTS) then in marshmallow only taking permission in the MANIFEST will never be enough. You have to take user’s permission in the run time(in JAVA) only when the application is going to execute the specific feature.

Application permission in run time will only work in android OS 6 (Marshmallow) and above.

Add Permission to Manifest.java

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

Try my code

private static final int PERMISSION_REQUEST = 100;

//this is the onclick listener of send button
public void send(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
                Snackbar.make(findViewById(R.id.rl), "You need to grant SEND SMS permission to send sms",
                        Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
                    }
                }).show();
            } else {
                requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);
            }
        } else {
            sendSMS();
        }
    } else {
        sendSMS();
    }
}

private void sendSMS() {
    String phoneNumber = "1234";
    String msg="hello, this is a text message";
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, msg, null, null);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        Snackbar.make(findViewById(R.id.rl), "Permission Granted",
                Snackbar.LENGTH_LONG).show();
        sendSMS();

    } else {

        Snackbar.make(findViewById(R.id.rl), "Permission denied",
                Snackbar.LENGTH_LONG).show();

    }
}

To prompt user to grant or deny the permission, you can write the following code and it will automatically show a dialog to the user and give him/her the option to allow or deny. please mark that this dialog is system defined and uneditable.

requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST);

Output will be like