Android:Alert Dialog消失并执行下一个Intent

时间:2011-06-16 22:39:30

标签: android alertdialog

我正在使用禁用GPS的AlertDialog,一旦用户启用了GPS,我就会通过Intent移动到另一个Activity。问题是AlertDialog出现,然后在我点击Dialog上的任何按钮之前移动到下一个活动。 我需要做什么才能使下一个Intent仅在我对AlertDialog执行操作时执行? 这是我的代码:

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);
}

提前感谢您的所有答案!

2 个答案:

答案 0 :(得分:0)

看起来问题是虽然未启用GPS位置,但您仍然在myLocation.getLocation中获取位置。

在调用createGpsDisabledAlert()之后,您可能应该返回而不是继续使用该方法。

答案 1 :(得分: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);
    }
}