2011-06-16 65 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()之后,您可能应该返回而不是继续该方法。