我正在尝试使用此代码单击按钮时显示用户当前位置:
mButton = (Button) findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String[] permissions;
permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this, permissions, 4);
else {
mButton.setText("");
// request and display location
}
}
});
到目前为止,非常好,但是当我检查权限结果(onRequestPermissionsResult
)时,我正在使用for
语句来迭代permissions
数组并在内部创建一个对话框解释为什么需要当前的许可。问题是 - 因为所需的权限是ACCESS_FINE_LOCATION
和ACCESS_COARSE_LOCATION
for
语句运行两次,这是我的代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (requestCode == 4) {
for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
boolean showRationale = shouldShowRequestPermissionRationale(permission);
if (!showRationale) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The application must have permission to find location");
builder.setTitle("Location");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
builder.setNegativeButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
} else if (Manifest.permission.ACCESS_COARSE_LOCATION.equals(permission) || Manifest.permission.ACCESS_FINE_LOCATION.equals(permission)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The application must have permission to find location");
builder.setTitle("Location");
builder.setNegativeButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String[] permissionss = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
ActivityCompat.requestPermissions(MainActivity.this, permissionss, 4);
}
});
builder.show();
}
Toast.makeText(MainActivity.this, "No permission to use location", Toast.LENGTH_LONG).show();
} else if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED)
// request and display location
}
}
}
}
我想到了许多解决方案,只在权限String[]
数组中传递了一个权限,只显示if
语句if(i==0)
的对话框。我不知道这个问题的正确解决方案是什么。谢谢。
P.S - 对不起我的英文。
新的最终工作代码与图书馆:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
requestLocation();
else
MainActivityPermissionsDispatcher.requestLocationWithCheck(MainActivity.this);
}
});
@NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
public void requestLocation() {
double lo = 0.0, la = 0.0;
removeItem();
myToast = Toast.makeText(MainActivity.this, lo + " , " + la, Toast.LENGTH_LONG);
myToast.show();
// Get Location And Display It Using Toast.
showQuestion("Location");
}
@OnNeverAskAgain({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void showNeverAskForLocation() {
Toast.makeText(this, R.string.permission_location_neverask, Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The application must have permission to find location");
builder.setTitle("Location");
builder.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", MainActivity.this.getPackageName(), null);
intent.setData(uri);
MainActivity.this.startActivity(intent);
}
});
builder.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
@OnShowRationale({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void showRationaleForLocation(final PermissionRequest request) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("The application must have permission to find location");
builder.setTitle("Location");
builder.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
request.proceed();
}
});
builder.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(@NonNull DialogInterface dialog, int which) {
request.cancel();
}
});
builder.show();
}
@OnPermissionDenied({Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
void showDeniedForLocation() {
Toast.makeText(this, R.string.permission_location_denied, Toast.LENGTH_SHORT).show();
}