我正在使用禁用GPS的AlertDialog,一旦用戶啓用GPS,我將通過Intent移動到另一個活動。問題是出現AlertDialog,然後移動到下一個活動,然後我可以單擊對話框上的任何按鈕。 我需要做什麼才能讓下一個Intent只執行一次我在AlertDialog上執行的操作? 這裏是我的代碼:Android:警報對話框消失並執行下一個Intent
public void OnClickNearMe(View view) {
LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
createGpsDisabledAlert();
}
Location locationResult = null;
MyLocation myLocation = new MyLocation();
boolean locationEnabled = myLocation.getLocation(this, locationResult);
if (locationEnabled == true) {
locationResult = myLocation.getLocationResult();
showResultsScreen(locationResult);
} else
Toast.makeText(this, R.string.noLoc, Toast.LENGTH_LONG).show();
return;
}
private void createGpsDisabledAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS is disabled! Would you like to enable it?")
.setCancelable(false)
.setPositiveButton("Enable GPS",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showGpsOptions();
}
});
builder.setNegativeButton("Do nothing",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
private void showGpsOptions() {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
private void showResultsScreen(Location locationResult) {
Intent resultsIntent = new Intent(this, ResultScreenList.class);
startActivity(resultsIntent);
}
在此先感謝您的答案!
謝謝你,工作。有什麼方法可以讓用戶退出設置頁面,而不必按下後退按鈕?這是我的初衷。 – Sushma 2011-06-17 14:52:55
想到這些並不太明顯。你也許可以彈出一杯祝酒詞,指導用戶在完成後向他們反饋,但除此之外,我對此沒有任何明智的想法。 – kabuko 2011-06-17 18:09:06