Cool Java guy Cool Java guy - 1 month ago 8
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;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
mLayout = findViewById(R.id.main_layout);

showAllSMS();
}

@Override
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.",
Snackbar.LENGTH_SHORT)
.show();
readSMS();
} else {
// Permission request was denied.
Snackbar.make(mLayout, "Camera permission request was denied.",
Snackbar.LENGTH_LONG)
.show();

}
}
// 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
Snackbar.make(mLayout,
"Camera permission is available. Starting preview.",
Snackbar.LENGTH_LONG).show();
readSMS();
} else {
Log.i(TAG, "================= 2");
// Permission is missing and must be requested.
requestReadSMSPermission();
}
// 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() {
@Override
public void onClick(View view) {
// Request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_SMS},
PERMISSION_REQUEST_SMS);
}
}).show();

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

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.

Answer

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

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