android 自动消失对话框,Android:警报对话框消失并执行下一个Intent

我使用的是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);

}

预先感谢您的所有答复!!!

最佳答案

显示对话框后,即使单击“确定”或“取消”,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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值