简体   繁体   English

使应用程序等到定位服务已打开?

[英]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.

相关问题 如何使应用程序等待线程执行完毕? - How to make the application wait until a thread has executed? Selenium [Java]:如何让它等到表被刷新? - Selenium [Java] : How can I make it wait until a table has been refreshed? 如何使Tomcat等到Web应用程序完成? - How to make Tomcat wait until web application finishes? 如何使用位置服务关闭当前位置 - how to get current location using location services turned off 我如何等待,直到RabbitMQ消息已同步到集群中的其他节点? - How can i wait until a RabbitMQ message has been synced to the other nodes in the cluster? 我无法让程序等到GUI完成收集所需信息(Java) - I can't make my program to wait until GUI has finished gathering requested info (Java) 如何让线程等到另一个线程完成该方法的执行 - how to make thread wait until another thread has completed that method execution 如何使Java程序不断检查是否有更改并循环进行直到出现 - How to make a Java program to constantly check if there has been changes and loop it until there is 如何使Java程序暂停直到按下特定按钮? - How can I make my Java program pause until a specific button has been pressed? Java如何等待命令完成? - Java how to wait until a command has completed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM