如果用户单击“访问”权限,我请求用户访问或拒绝gps权限,然后我的代码正在运行,但当用户点击“拒绝”权限时,我无法打开其他活动
这是我的代码:
txtLocation = (TextView)findViewById(R.id.txtLocation);
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location;
if (network_enabled) {
boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED;
Log.d("permission", "onCreate: "+permissionGranted);
if(permissionGranted) {
// {Some Code}
} else {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION},
200);
}
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, "code", Toast.LENGTH_SHORT).show();
switch (requestCode) {
case 200: {
// If request is cancelled, the result arrays are empty.
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(MainActivity.this, "Granted", Toast.LENGTH_SHORT).show();
}else if(grantResults[0] == PackageManager.PERMISSION_DENIED) {
Intent intent = new Intent(this,Homepage.class);
startActivity(intent);
}
}
default:{
Toast.makeText(MainActivity.this, "Denied", Toast.LENGTH_SHORT).show();
}
// other 'case' lines to check for other
// permissions this app might request
}
}
答案 0 :(得分:0)
您正在申请两项权限:
onActivityResult()
但在grantResults[0]
中你只看String permissions[], int[] grantResults
。
这些是数组:ActivityCompat.requestPermissions().
。
全部检查。
更新
CommonEngine.DataTableToCsv()
你有两次代码。但是你评论的第一个。第二个是在错误的地方。仅使用第一个。