我的Android应用中有一个Google Maps Activity,它有一个AlertDialog,当GPS禁用时,该对话框会调用GPS设置,但该对话框未显示。
然后发生的是,我得到一个没有标记的Google Maps活动,因为对话框没有要求我激活GPS。
private void openSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
builder.setTitle("Permissões requeridas");
builder.setMessage("Esta app precisa de permissões de GPS. Dê essas permissões nas definições.");
builder.setPositiveButton("Definições", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 101);
}
});
builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
这是我打电话给openSettingsDialog()
的地方private void requestMultiplePermissions() {
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
Toast.makeText(getApplicationContext(), "Todas as permissões aceites!", Toast.LENGTH_SHORT).show();
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
// show alert dialog navigating to Settings
openSettingsDialog();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Algum erro ocorreu! ", Toast.LENGTH_SHORT).show();
}
})
.onSameThread()
.check();
}
OnCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//code for getting current location
requestMultiplePermissions();
tvLocation = (TextView) findViewById((R.id.tv));
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String gpslocation = extras.getString("GPSLocation");
String[] latLng = gpslocation.split(",");
double latitude = Double.parseDouble(latLng[0]);
double longitude = Double.parseDouble(latLng[1]);
LatLocation = new LatLng(latitude, longitude);
}
}
答案 0 :(得分:0)
在openSettingDialogMethod()
删除
builder.show();
并添加此行代码
AlertDialog alertDialog = builder.create();
alertDialog.show();
答案 1 :(得分:0)
好,我修复了它。显然我想要的不是该对话框,而是“打开GPS设置”对话框,现在我可以在我的App中使用它。很抱歉,我是Android Studio的新手。