Cool Java guy Cool Java guy - 4 months ago 28
Android Question

Android Runtime permission always denied

I am trying to read sms on Android Level-23 but it does not show the run time permission. What is wrong with it.

My code is

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final int PERMISSION_REQUEST_SMS = 0;
private static final String TAG = MainActivity.class.getName();

private View mLayout;

protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main );
mLayout = findViewById(;


public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
Log.i(TAG, "onRequestPermission Result = "+grantResults[0]);
if (requestCode == PERMISSION_REQUEST_SMS) {
// Request for camera permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "onRequestPermission Result 1");
// Permission has been granted. Start camera preview Activity.
Snackbar.make(mLayout, "Camera permission was granted. Starting preview.",
} else {
// Permission request was denied.
Snackbar.make(mLayout, "Camera permission request was denied.",

// END_INCLUDE(onRequestPermissionsResult)

private void showAllSMS() {
Log.i(TAG, "=================");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
== PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "================= 1");
// Permission is already available, start camera preview
"Camera permission is available. Starting preview.",
} else {
Log.i(TAG, "================= 2");
// Permission is missing and must be requested.
// END_INCLUDE(startCamera)

* Requests the {@link android.Manifest.permission#CAMERA} permission.
* If an additional rationale should be displayed, the user has to launch the request from
* a SnackBar that includes additional information.
private void requestReadSMSPermission() {
// Permission has not been granted and must be requested.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_SMS)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// Display a SnackBar with a button to request the missing permission.
Snackbar.make(mLayout, "Camera access is required to display the camera preview.",
Snackbar.LENGTH_INDEFINITE).setAction("OK", new View.OnClickListener() {
public void onClick(View view) {
// Request the permission
new String[]{Manifest.permission.READ_SMS},

} else {
"Permission is not available. Requesting camera permission.",
// Request the permission. The result will be received in onRequestPermissionResult().
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA},

private void readSMS() {
Log.d(TAG, "REad smsm");

It always shows me "Camera permission request was denied.". It is copied from the Android sample. The real code with camera permission work absolutely fine.

Please explain.


change this line of code, it seems you are requesting camera permission instead of SMS read and recieve permission, and it is not added in your manifest.

 ActivityCompat.requestPermissions(this, new String[]     {Manifest.permission.CAMERA},

And please clean up your code, everyone thought you want to use the camera.