如何正确提示用户打开“提高位置准确性”设置?

时间:2019-07-01 04:52:50

标签: android

我的应用程序要求用户在其设置中启用“提高的位置准确性”。不幸的是,我无法找出正确的方法来真正进入正确的菜单,并且一旦设置完成就让后退按钮返回我的应用程序。

这是我所拥有的:

    private fun checkLocationSettingsAsync() {
        viewModel.launch(Dispatchers.IO) {
            val locationSettings = client.checkLocationSettings(
                locationBuilder.build()
            ).asDeferred()

            locationSettings.asTask().addOnSuccessListener {
                //location settings are already high accuracy
                locationSettingsOkay = true
            }.addOnFailureListener { e ->
                if (e is ResolvableApiException) {
                    //location settings not satisfied, request to make them higher
                    runCatching {
                        val intent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                        startActivityForResult(intent, LOCATION_ACCURACY_ACCESS_CODE)
                    }.onFailure {
                        println(it.stackTrace)
                    }
                }
            }

            locationSettings.await()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            LOCATION_ACCURACY_ACCESS_CODE -> {
                if (resultCode == Activity.RESULT_OK) {
                    //TODO: navigate back to activity once setting is set
                }
            }
        }
    }

Android菜单Settings.ACTION_LOCATION_SOURCE_SETTINGS转到包含“改进的位置准确度”设置的外部菜单,但是我实际上找不到正确的Intent名称来获取适当的菜单,也无法获取,因此启用设置后,用户可以导航回我的应用程序。

有什么建议吗?还是文档,因为我什么都找不到!

编辑:这是一个很好的代码示例! https://github.com/googlesamples/android-play-location/blob/master/LocationUpdates/app/src/main/java/com/google/android/gms/location/sample/locationupdates/MainActivity.java

4 个答案:

答案 0 :(得分:5)

以下代码用于提示用户更改位置设置 Click here to see full document

task.addOnSuccessListener { locationSettingsResponse ->
   // All location settings are satisfied. The client can initialize
   // location requests here.    
}
task.addOnFailureListener { exception ->
 if (exception is ResolvableApiException){
     // Location settings are not satisfied, but this can be fixed
     // by showing the user a dialog.
     try {
         // Show the dialog by calling startResolutionForResult(),
         // and check the result in onActivityResult().
         exception.startResolutionForResult(this@MainActivity,
                 REQUEST_CHECK_SETTINGS)
     } catch (sendEx: IntentSender.SendIntentException) {
         // Ignore the error.
     }
 }
}

答案 1 :(得分:1)

这是我的方法

GPS设置不好吗?

  1. 使用
  2. 向用户显示“打开Gps对话框”

来自活动:

        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"python", fileName)
        {
            RedirectStandardError = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            WorkingDirectory = @"c:\temp"
        };
        p.Start();

        p.WaitForExit();
        string output = p.StandardOutput.ReadToEnd();
        string error = p.StandardError.ReadToEnd();

来自片段:

status.startResolutionForResult(this , REQUEST_CHECK_SETTINGS);
  1. startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CHECK_SETTINGS, null, 0, 0, 0, null) 中寻找结果

如果您要进行HIGH_ACCURACY升级,请提防此错误

Not Able To Upgrade Gps settings to high Accuracy using FusedLocationProvider In some devices

尽管我在上次更新Google Play服务后没有遇到此错误。.....但是我不确定此错误是否已完全修复

如果resultCode为RESULT_OK->不执行任何操作

如果resultCode为RESULT_CANCELED->向用户显示一个对话框,提示他需要手动升级Gps优先级 并使用

将用户重定向到位置设置页面
onActivityResult

答案 2 :(得分:0)

这可能会帮助您:

很抱歉,如果它在Java中。让我知道它是否有效。

 public static void displayPromptForEnablingGPS(final Activity activity, Context context) {
        LocationManager lm = (LocationManager) activity.getSystemService(LOCATION_SERVICE);
        if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(R.string.locationTitle);
            builder.setMessage(R.string.enableLocation);
            builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {

                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    activity.startActivity(intent);
                }
            });
            Dialog alertDialog = builder.create();
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }
    }

答案 3 :(得分:0)

这是我意识到需要使用startIntentSenderForResult而不是startResolutionForResult因为我是从一个片段启动它时想到的。 :-)

    private fun checkLocationSettings() {
        settingsClient.checkLocationSettings(locationSettingsRequest).addOnSuccessListener {
            //all settings satisfied!
            locationSettingsOkay = true
        }.addOnFailureListener { e ->
            val statusCode = (e as ApiException).statusCode
            when (statusCode) {
                LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                    println("settings not good enough, attempting to upgrade them")
                    try {
                        val rae = (e as ResolvableApiException)
                        startIntentSenderForResult(
                            rae.resolution.intentSender,
                            REQUEST_CHECK_SETTINGS,
                            null, 0, 0, 0, null
                            )
                    } catch (sie : IntentSender.SendIntentException) {
                        println("pending intent unable to process request")
                    }
                }
                LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                    Toast.makeText(
                        this@PrimerFrag.requireContext(),
                        "Location settings can't be fixed here. Try to turn on Improved Location Accuracy in settings.",
                        Toast.LENGTH_LONG
                        ).show()
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            REQUEST_CHECK_SETTINGS -> {
                when (resultCode) {
                    Activity.RESULT_OK -> {
                        locationSettingsOkay = true
                        if (locationPermissionGranted && resultSuccess) {
                            makeButtonsReadyAndSubmitList(resultList, destinationsAdapter)
                        }
                    }
                    Activity.RESULT_CANCELED -> {
                        //TODO: ask for location settings again
                    }
                }
            }
        }
    }