我编写了一个示例代码,以便于理解运行时权限。它工作正常。但我面临的问题是,如果用户在任何阶段单击“永不询问权限”,即使在卸载后重新安装/全新安装,App也会停止询问权限。这是它的工作方式还是我缺少的东西。我还提到Android M - check runtime permission - how to determine if the user checked "Never ask again"?很好地解释了这些事情,但仍然想澄清我对它的困惑。
这是代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView te1;
te1 = (TextView) findViewById(R.id.te1);
te1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(MainActivity.this);
builder.setTitle("Title");
builder.setMessage("Fill Form")
.setCancelable(false) //false will disable clicking anywhere in screen during alter.
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
})
.setNegativeButton("Do it Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
android.app.AlertDialog alert = builder.create();
alert.show();
}else {
Toast.makeText(MainActivity.this, "You Selected Never Asked this Permission again", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MainActivity.this, "Intent Fired", 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) {
Toast.makeText(this, "App Need to access GPS to get your location which you can share", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "You did not get permission.Do Nothing", Toast.LENGTH_SHORT).show();
}
}
}
}
答案 0 :(得分:0)
AFAIK:
shouldShowRequestPermissionRationale()
如果应用先前已请求权限且用户拒绝了请求,则此选项将返回true - 如果用户选择“再也不再请求此权限”,则会返回。
如果用户之前拒绝了该请求,则该用户只会看到“再也不会要求此权限”。
即使在按下“再也没有请求此权限”之后,您是否仍然在请求权限时回复onRequestPermissionsResult?
如果是这样,您可以监听该回调并请求用户直接通过设置更改权限。
您可以使用以下方法将用户定向到设置。
public static void goToAppSettings(@NonNull final Context context) {
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}