【问题标题】:Make the application wait until the location services has been turned on?让应用程序等到位置服务已打开?
【发布时间】: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


    【解决方案1】:

    您可以在自己的情况下轻松使用startActivityForResult

    当您开始设置以启用您的位置时,您可以像这样启动意图。

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

    现在,当您从位置设置返回时,您会在此处收到回调。

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

    简单!

    【讨论】:

    • 太棒了。您能否也告诉我“imageChooserIntent”值指的是什么?
    • 抱歉,已编辑答案。应该是locationSettingIntent
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多