public void onClick(View v) {
if(v.getId() == R.id.btnSave) {
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_PERMISSION_STORAGE);
} else {
//do something
}
}
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_STORAGE
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
}
我尝试使用构建变体调试来调试此代码,调用函数onRequestPermissionsResult 但是随着版本的发布,此函数onRequestPermissionsResult不会被调用
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setLocationSendListener(this);
takeRunTimePermission();
}
public void takeRunTimePermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
1);
}
@Override 公共无效onRequestPermissionsResult(int requestCode, 字符串权限[],整数[] grantResults){ 开关(requestCode){ 情况1:{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
} else {
// permission denied, boo! Disable the
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// permissions this app might request
}
}