Я прошу пользователю получить доступ или отказать в выдаче разрешения 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
}
}
Что вы имеете в виду, поскольку не можете открыть? Это крушение? –
@KamranAhmed Нет, это не сбой, но мой код не вызывается, если Access запрещен, я не могу получить доступ к отрицаемой части в моем коде – bipin
, вы можете использовать debug, чтобы узнать, что не так. –