【发布时间】:2016-12-21 04:10:59
【问题描述】:
我有一个依赖于已启用位置服务的应用程序。
因此,当用户启动应用程序时,会出现一个对话框,它会检查位置服务是否已启用。但是,我希望应用程序暂停,直到用户转到设置页面(他在对话框中单击“确定”时被重定向到该页面)并启用位置服务。一旦他这样做了,他应该能够返回到MainActivity,并且代码应该从他离开的地方继续。
如果我不让应用暂停,代码会继续并尝试执行需要开启位置服务的代码,然后应用就会崩溃。
我目前有这个,那么我该如何修改它以使其等待?
if(!location_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("Location services are currently not " +
"enabled. You must enable this in order to continue. Would you like to do this now?");
dialog.setPositiveButton("Take me to location services", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
// WAIT UNTIL LOCATION SERVICES ENABLED
}
});
dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
//EXIT APPLICATION
}
});
dialog.show();
}
if(location_enabled) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
//DO FANCY STUFF WITH LOCATION
}
}
【问题讨论】:
标签: java android multithreading