2011-06-16 66 views
0

我正在使用禁用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); 
} 

在此先感謝您的答案!

回答

0

顯示對話框後,createGpsDisabledAlert將繼續並完成,即使在單擊確定或取消之前。也許會將OnClickNearMe中的其餘代碼重構爲其他方法,並且只有在未啓用位置的情況下才會調用它,並且還可以在設置頁面後調用它。也許是這樣的:

public void OnClickNearMe(View view) { 
    LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
     createGpsDisabledAlert(); 
    } else { 
     getLocation(); 
    } 
} 

private void getLocation() { 
    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(); 
    } 
} 

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(); 
        getLocation(); 
       } 
     }); 
     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); 
    } 
} 
+0

謝謝你,工作。有什麼方法可以讓用戶退出設置頁面,而不必按下後退按鈕?這是我的初衷。 – Sushma 2011-06-17 14:52:55

+0

想到這些並不太明顯。你也許可以彈出一杯祝酒詞,指導用戶在完成後向他們反饋,但除此之外,我對此沒有任何明智的想法。 – kabuko 2011-06-17 18:09:06

0

看起來問題是,雖然GPS位置未啓用,但您仍然在myLocation.getLocation中獲取位置。

在您調用createGpsDisabledAlert()之後,您可能應該返回而不是繼續該方法。