代码只显示第一个权限的对话框,而不显示其他两个权限。如何以更简单的方式处理多个请求,其中应用程序一次提示每个权限。 / p>
请求权限的代码如下:
package com.example.hp.requesting_permissions;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
} else {
Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 2);
}
} else {
Toast.makeText(MainActivity.this, "denied", Toast.LENGTH_SHORT).show();
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.INTERNET)) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET}, 3);
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET}, 3);
}
} else {
Toast.makeText(MainActivity.this, "denied", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
}
}
}
break;
case 2: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
}
}
}
break;
case 3: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
}
}
}
break;
}
}
}
答案 0 :(得分:0)
此处询问所有权限
public class RequestUserPermission {
private Activity activity;
// Storage Permissions
private static final int REQUEST_PERMISSION = 1;
private static String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
public RequestUserPermission(Activity activity) {
this.activity = activity;
}
public boolean verifyStoragePermissions() {
// Check if we have write permission
int permissionWrite = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionRead = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
if ((permissionWrite != PackageManager.PERMISSION_GRANTED) ||
(permissionRead != PackageManager.PERMISSION_GRANTED)) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS,
REQUEST_PERMISSION
);
return false;
}else{
return true;
}
}
public void providePermission(){
verifyStoragePermissions();
}
public void forceFullOpenPermission(){
if(!verifyStoragePermissions()){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivityForResult(intent, REQUEST_PERMISSION);
}
}
}
答案 1 :(得分:0)
喜欢这个
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SelectUserActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_SMS
},
10);
return;
}
}
答案 2 :(得分:0)
您可以根据需要更改PERMISSIONS_ARR
,也可以根据需要自定义此代码。
在课堂顶部添加这些代码 - 全局
private static final int REQUEST_PERMISSION = 1;
private static String[] PERMISSIONS_ARR = {
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.PACKAGE_USAGE_STATS
};
请在您要求许可的下方调用
public void askForPermission()
{
// Verify that all required contact permissions have been granted.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, PERMISSIONS_ARR, REQUEST_PERMISSION);
}
else
{
// Permission already given do whatever you want...
}
}
添加以下代码以跟踪用户授予的权限以及拒绝的权限。根据这个做出决定。
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == REQUEST_PERMISSION)
{
// for each permission check if the user grantet/denied them
// you may want to group the rationale in a single dialog,
// this is just an example
int count = 0;
for (int i = 0, len = permissions.length; i < len; i++)
{
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
{
count++;
}
else
{
//Permission denied you can ask it for again.
}
}
if (count == permissions.length)
{
// All permission granted
}
else
{
// Not all permission granted, controlled launch of application.
}
}
}