Android: диалоговое окно оповещения исчезает и выполняет следующее намерение - PullRequest
0 голосов
/ 17 июня 2011

Я использую отключенный GPS AlertDialog, и как только пользователь активирует GPS, я перехожу к другому действию через намерение. Проблема в том, что AlertDialog появляется, а затем переходит к следующему действию, прежде чем я могу нажать на любую кнопку в диалоге. Что мне нужно сделать, чтобы следующее намерение выполнялось только после того, как я выполнил действие на 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 голосов
/ 17 июня 2011

После того, как вы откроете диалоговое окно, createGpsDisabledAlert продолжится и завершится даже до того, как вы нажмете OK или Отмена. Возможно, переделайте оставшуюся часть кода в 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 голосов
/ 17 июня 2011

Похоже, проблема в том, что, хотя местоположение GPS не включено, вы по-прежнему получаете местоположение в myLocation.getLocation.

После вызова createGpsDisabledAlert () вы, вероятно, должны вернуться вместо продолженияс методом.

...