我的Android应用程序中有一个谷歌地图活动,它有一个AlertDialog,当禁用GPS时调用GPS设置,但该对话框没有显示。
然后会发生什么事情,我得到一个没有标记的Google地图活动,因为Dialog没有要求我激活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);
}
}
在openSettingDialogMethod()中
去掉
builder.show();
并添加此行代码
AlertDialog alertDialog = builder.create();
alertDialog.show();
好的,我修好了。显然我想要的不是那个对话框,而是我现在在我的应用程序中使用的Open GPS Settings对话框。对不起,我是Android Studio中的新手。