Ich versuche, SMS auf Android-Level-23 zu lesen, aber es zeigt nicht die Berechtigung Laufzeit. Was ist daran falsch.Android Runtime-Berechtigung immer verweigert
Mein Code ist
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");
}
}
Es zeigt mir immer "Camera Erlaubnisanfrage verweigert wurde.". Es wird vom Android-Beispiel kopiert. Der echte Code mit Kamera-Erlaubnis funktioniert absolut gut.
Bitte erläutern.
haben Sie die Berechtigung in Manifest –
Haben Sie die Berechtigung in der AndroidManifest.xml ?? – user3024215
Ja, tat ich. Ich habe die Berechtigung in manifest.xml –