我有以下帮助方法来检查权限:
private boolean canAccessLocation() {
return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
}
private boolean hasPermission(String perm) {
return(PackageManager.PERMISSION_GRANTED==checkCallingOrSelfPermission(perm));
}我有一个请求方法来提示用户访问他们的位置。
public void requestLocationPermissions(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
Log.d("permissions",
"Displaying contacts permission rationale to provide additional context.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_LOCATION);
}
}在我需要位置许可的地方,我写
if(!canAccessLocation()){
requestLocationPermissions();
} else {
}
startActivity(new Intent(MyActivity.this, MyOtherActivity.class));我遇到的问题是,在向用户显示对话框以允许或拒绝权限之前,新的活动是通过意图启动的。因此,如果我在下一个活动中有要求用户授予或拒绝权限的代码,它将崩溃,然后询问用户是否希望授予权限。让API 23权限系统在这个应用程序上正常工作我遇到了很多困难,我真的需要一些帮助。
因此,我的问题是:在用户选择是否拒绝或允许权限之前,我如何阻止后续代码行的执行?
发布于 2016-06-15 21:37:27
您确实需要在请求权限的活动中实现此回调。
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults);在该回调中,只需验证requestCode是您请求的内容,如果是grantResults == PackageManager.PERMISSION_GRANTED,则在授予权限时执行您需要做的事情,比如生成一个新的活动。
此外,如果ActivityCompat.shouldShowRequestPermissionRationale调用返回true,则应该向用户显示一个提示,说明请求权限的理由。一旦用户按下'Ok',您将再次请求权限。
发布于 2017-01-25 14:54:17
我为此挣扎了几个小时,所以我只想把它放在那里,以防它对某人有用。
我已经修改了来自:https://developer.android.com/training/permissions/requesting.html的代码
在我的例子中,应用程序需要请求多个(“危险”)权限。当应用程序启动时,我会这样做,然后再显示启动屏幕。
我应该指出,我没有运气同时请求所有的权限,因此出现了递归。希望,这是可能的,有人可以弄清楚这一点。此外,我也评论了“我们是否应该给出一个解释”这句话。下面的逻辑不适合这种行为,但我把它留给后人,因为它来自于上面的链接。
package a.dangerous.app;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
public class AskMultiplePermsThenSpashActivity extends Activity {
static final String[] _requestPermissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_SMS,
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS
};
@Override
protected void onStart() {
super.onStart();
this.checkPermissions(0); //start at zero, of course
}
private void checkPermissions(int permissionIndex) {
if(permissionIndex >= _requestPermissions.length) {
//i.e. we have requested all permissions, so show the splash screen
this.showSplash();
}
else {
this.askForPermission(_requestPermissions[permissionIndex], permissionIndex);
}
}
private void askForPermission(String permission, int permissionIndex) {
if (ContextCompat.checkSelfPermission(
this,
permission)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
// if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(
this,
new String[]{permission},
permissionIndex //permissionIndex will become the requestCode on callback
);
}
else {
this.checkPermissions(permissionIndex+1); //check the next permission
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//Regardless of whether the permission was granted we carry on.
//If perms have been denied then the app must cater for it
this.checkPermissions(requestCode+1); //check the next permission
}
private void showSplash() {
//(ta da)
//once splashed, start the main activity
this.startMainActivity();
}
private void startMainActivity() {
Intent mainIntent = new Intent(
this,
MainActivity.class
);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.startActivity(mainIntent);
this.finish();
}
}发布于 2016-06-15 21:14:12
如果发布的片段正是您在项目中尝试的内容,那么解决方案很简单:D
if(!canAccessLocation()){
requestLocationPermissions();
} else {
//start only if permission is granted
startActivity(new Intent(MyActivity.this, MyOtherActivity.class));
}https://stackoverflow.com/questions/37845802
复制相似问题