如果 activity 停止并再次启动,请求启用 GPS 发生两次
Request to enable GPS occuring twice if activity stopped and started again
我有一个 activity,它要求用户通过显示带有“确定”和“取消”按钮的对话框在 onStart() 中启用 GPS。但是,如果用户在与对话框交互之前按下主页按钮,然后再次启动应用程序,则会在旧对话框之上出现一个新对话框,因此用户会一个接一个地看到两个对话框。如何关闭 activity?
的 onStop 方法中的旧对话框
这是我的代码的相关部分:
protected void onStart() {
super.onStart();
enableGPS();
}
private void enableGPS() { //GPS ENABLE RELATED METHOD
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result
.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS: //GPS ALREDY TURNED ON
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
else {
redirectStatus.setText("ERROR: Not able to connect to Location Service");
btnRetry.setVisibility(View.VISIBLE);
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: //GPS NOT ALREDY TURNED ON, SHOW TURN ON DIALOG
try {
status.startResolutionForResult(enableGPSActivity.this, REQUEST_CHECK_SETTINGS); //CREATING DIALOG FOR GPS TURN ON
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //GPS CANNOT BE TURNED ON
redirectStatus.setText("Permission Available, not able to enable GPS");
btnRetry.setVisibility(View.VISIBLE);
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { //GPS ENABLE RELATED METHOD, DIALOG FOR GPS TURN ON
if (requestCode == REQUEST_CHECK_SETTINGS) {
if (resultCode == Activity.RESULT_OK) { // GPS TURN ON ACCEPTED
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
redirectStatus.setText("Please Wait. Fetching Your Current Location");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
}
if (resultCode == Activity.RESULT_CANCELED) { //GPS TURN ON REJECTED
redirectStatus.setText("Permission Available, GPS Off");
btnRetry.setVisibility(View.VISIBLE);
}
}
}
}
// declares variables name
private AlertDialog gpsAlertDialog;
private boolean isGpsDialogShowing = false;
private LocationManager manager;
// check gps enable or not if enable than remove dialog else show gps enable dialog in onResume method
protected void onResume()
{
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)&&!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
ShowGpsDialog();
} else {
removeGpsDialog();
}
}
// remove dialog on destroy activity
@Override
protected void onDestroy() {
super.onDestroy();
removeGpsDialog();
}
// method for show dialog for gps enable
private void ShowGpsDialog() {
isGpsDialogShowing = true;
AlertDialog.Builder gpsBuilder = new AlertDialog.Builder(
MainMenuDrawerActivity.this);
gpsBuilder.setCancelable(false);
gpsBuilder
.setTitle(getString(R.string.dialog_no_gps))
.setMessage(getString(R.string.dialog_no_gps_messgae))
.setPositiveButton(getString(R.string.dialog_enable_gps),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// continue with delete
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
removeGpsDialog();
}
})
.setNegativeButton(getString(R.string.dialog_exit),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// do nothing
removeGpsDialog();
finish();
}
});
gpsAlertDialog = gpsBuilder.create();
gpsAlertDialog.show();
}
// method for remove gps dialog
private void removeGpsDialog() {
if (gpsAlertDialog != null && gpsAlertDialog.isShowing()) {
gpsAlertDialog.dismiss();
isGpsDialogShowing = false;
gpsAlertDialog = null;
}
}
将 enableGPS();
调用到 onCreate()
而不是 onStart()
。
因为,
根据Activity
的生命周期 - 当用户按下主页按钮时,当前activity将进入后台通过调用 - onPause()
和 onStop()
方法 。 当您从 Stack 恢复 activity 时 - Activity
将通过调用 onRestart()
、onStart()
和 恢复=19=].
我有一个 activity,它要求用户通过显示带有“确定”和“取消”按钮的对话框在 onStart() 中启用 GPS。但是,如果用户在与对话框交互之前按下主页按钮,然后再次启动应用程序,则会在旧对话框之上出现一个新对话框,因此用户会一个接一个地看到两个对话框。如何关闭 activity?
的 onStop 方法中的旧对话框这是我的代码的相关部分:
protected void onStart() {
super.onStart();
enableGPS();
}
private void enableGPS() { //GPS ENABLE RELATED METHOD
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi
.checkLocationSettings(mGoogleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result
.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS: //GPS ALREDY TURNED ON
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
else {
redirectStatus.setText("ERROR: Not able to connect to Location Service");
btnRetry.setVisibility(View.VISIBLE);
}
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: //GPS NOT ALREDY TURNED ON, SHOW TURN ON DIALOG
try {
status.startResolutionForResult(enableGPSActivity.this, REQUEST_CHECK_SETTINGS); //CREATING DIALOG FOR GPS TURN ON
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //GPS CANNOT BE TURNED ON
redirectStatus.setText("Permission Available, not able to enable GPS");
btnRetry.setVisibility(View.VISIBLE);
break;
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { //GPS ENABLE RELATED METHOD, DIALOG FOR GPS TURN ON
if (requestCode == REQUEST_CHECK_SETTINGS) {
if (resultCode == Activity.RESULT_OK) { // GPS TURN ON ACCEPTED
if (locationServiceConnected) {
if (ActivityCompat.checkSelfPermission(enableGPSActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
redirectStatus.setText("Please Wait. Fetching Your Current Location");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, enableGPSActivity.this);
}
}
}
if (resultCode == Activity.RESULT_CANCELED) { //GPS TURN ON REJECTED
redirectStatus.setText("Permission Available, GPS Off");
btnRetry.setVisibility(View.VISIBLE);
}
}
}
}
// declares variables name
private AlertDialog gpsAlertDialog;
private boolean isGpsDialogShowing = false;
private LocationManager manager;
// check gps enable or not if enable than remove dialog else show gps enable dialog in onResume method
protected void onResume()
{
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)&&!manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
ShowGpsDialog();
} else {
removeGpsDialog();
}
}
// remove dialog on destroy activity
@Override
protected void onDestroy() {
super.onDestroy();
removeGpsDialog();
}
// method for show dialog for gps enable
private void ShowGpsDialog() {
isGpsDialogShowing = true;
AlertDialog.Builder gpsBuilder = new AlertDialog.Builder(
MainMenuDrawerActivity.this);
gpsBuilder.setCancelable(false);
gpsBuilder
.setTitle(getString(R.string.dialog_no_gps))
.setMessage(getString(R.string.dialog_no_gps_messgae))
.setPositiveButton(getString(R.string.dialog_enable_gps),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// continue with delete
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
removeGpsDialog();
}
})
.setNegativeButton(getString(R.string.dialog_exit),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// do nothing
removeGpsDialog();
finish();
}
});
gpsAlertDialog = gpsBuilder.create();
gpsAlertDialog.show();
}
// method for remove gps dialog
private void removeGpsDialog() {
if (gpsAlertDialog != null && gpsAlertDialog.isShowing()) {
gpsAlertDialog.dismiss();
isGpsDialogShowing = false;
gpsAlertDialog = null;
}
}
将 enableGPS();
调用到 onCreate()
而不是 onStart()
。
因为,
根据Activity
的生命周期 - 当用户按下主页按钮时,当前activity将进入后台通过调用 - onPause()
和 onStop()
方法 。 当您从 Stack 恢复 activity 时 - Activity
将通过调用 onRestart()
、onStart()
和 恢复=19=].