[英]Make the application wait until the location services has been turned on?
I have an application that depends on location services being enabled. 我有一个依赖于启用位置服务的应用程序。
Thus, when the user starts the application- a dialog box appears and it checks if the location service is enabled or not. 因此,当用户启动应用程序时,将出现一个对话框,它检查是否启用了定位服务。 However, I want the application to pause until the user goes to the settings page (Which he is redirected to on clicking "okay" in the dialog box) and enables location services.
但是,我希望应用程序暂停,直到用户转到设置页面(在单击对话框中的“确定”后将重定向到该页面)并启用位置服务。 Once he does, he should be able to return back to the
MainActivity
and the code should continue where he left off. 完成后,他应该能够返回
MainActivity
,并且代码应该从他离开的地方继续。
If I don't let the app pause, the code just continues and tries to execute code that requires the location services to be on, and the app crashes. 如果我不让应用程序暂停,则代码将继续并尝试执行需要启用位置服务的代码,然后应用程序将崩溃。
I currently have this, so how can I modify it so that it waits? 我目前有这个,如何修改它以便等待?
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
}
}
You can easily use startActivityForResult
in your case. 您可以根据自己的情况轻松使用
startActivityForResult
。
When you're starting the settings to enable your location, you may start the intent like this. 当开始设置以启用您的位置信息时,您可以这样启动意图。
// Declare a global variable first
private final int ACTION_LOCATION_SETTING = 100;
// Now change the onClickListener like this
dialog.setPositiveButton("Take me to location services", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent locationSettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locationSettingIntent, ACTION_LOCATION_SETTING);
}
});
Now when you return from the location setting, you'll get a callback here. 现在,当您从位置设置返回时,您将在此处获得回调。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_LOCATION_SETTING:
if (resultCode == Activity.RESULT_OK) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ) {
//DO FANCY STUFF WITH LOCATION
}
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
Simple! 简单!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.