Android:警报对话框消失并执行下一个Intent [英] Android: Alert Dialog disappears and executes next Intent

查看:64
本文介绍了Android:警报对话框消失并执行下一个Intent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用禁用GPS的AlertDialog,一旦用户启用GPS,我便会通过一个Intent转到另一个Activity.问题是在我可以单击对话框上的任何按钮之前,出现AlertDialog,然后继续进行下一个活动. 我需要怎么做才能使下一个Intent仅在对AlertDialog执行动作后才执行? 这是我的代码:

I'm using a GPS disabled AlertDialog and once the user enables GPS, I move onto another Activity via an Intent. The problem is that the AlertDialog appears and then moves onto the next activity before I can click on any button on the Dialog. What do i need to do so that the next Intent executes only once I have performed the action on the AlertDialog? Here is my code:

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

在此先感谢您的回答!

推荐答案

显示对话框后,createGpsDisabledAlert将继续运行并完成操作,甚至在您单击确定"或取消"之前也是如此.也许将OnClickNearMe中的其余代码重构为另一种方法,并且仅在未启用位置的情况下才调用它,还可以在设置页面之后调用它.也许像这样:

After you show the dialog, createGpsDisabledAlert will continue on and finish even before you click OK or Cancel. Maybe refactor the rest of the code in OnClickNearMe to another method and call it only if location isn't enabled, and also call it after your settings page. Maybe something like:

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

这篇关于Android:警报对话框消失并执行下一个Intent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆