【发布时间】:2016-11-06 17:27:36
【问题描述】:
这是我的代码,如果我单击“允许”,它会询问我是否可以正常工作,但是当用户单击“拒绝”时,我想获得结果或记录被拒绝 如果用户单击“拒绝”权限,我该如何设置 这是我的代码:
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location;
if (network_enabled) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
;
}
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
1);
location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
lat = location.getLongitude();
lon = location.getLatitude();
Log.d("lat", "onCreate: "+lat+","+lon);
txtLocation.setText(+lat+","+lon);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Latitude:"+lat+"\n"+"Longitude:"+lon);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d("", "onRequestPermissionsResult: "+requestCode);
Toast.makeText(MainActivity.this, ""+grantResults.length, Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
// permission was granted, yay! Do the
} else {
Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
//user denied permission additionally you can check
//ActivityCompat.shouldShowRequestPermissionRationale if user checked on "do not show again" while clicking deny
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
【问题讨论】:
标签: android