小编Tyl*_*ler的帖子

如何在 Kotlin 中调用 FusedLocationProviderClient 的 getCurrentLocation() 方法

我正在尝试使用应用程序访问 Android 手机的位置信息,但似乎无法获取当前位置。我的代码如下:

fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireActivity())

val location_button = view.findViewById<ImageButton>(R.id.get_location)
location_button.setOnClickListener {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(requireActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED || ContextCompat.checkSelfPermission(requireActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
            val permission = arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION)
            requestPermissions(permission, LOC_PERMISSION_CODE)
        }
        else {
            //permission already granted
            fusedLocationClient.lastLocation
                .addOnSuccessListener { location ->
                    // Got last known location. In some rare situations this can be null.
                    val latitude = location?.latitude
                    val longitude = location?.longitude
                    println(latitude)
                    println(longitude)
                }
                .addOnFailureListener {
                    Toast.makeText(requireActivity(), "Failed on getting current location",
                        Toast.LENGTH_SHORT).show()
                } …
Run Code Online (Sandbox Code Playgroud)

android kotlin fusedlocationproviderclient

4
推荐指数
1
解决办法
4054
查看次数

为什么 bash“=~”运算符忽略指定模式的最后部分?

我试图将 bash 中的字符串与正则表达式模式进行比较,并发现了一些奇怪的东西。首先,我使用 GNU bash,版本 5.0.17(1)-release (x86_64-pc-linux-gnu)。这是在 WSL 范围内。

例如,这里是演示该问题的示例程序:

#!/bin/env bash

name="John"

if [[ "${name}" =~ "John"* ]]; then
    echo "found"
else
    echo "not found"
fi

exit
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,这将回显,found因为名称“John”与所描述的正则表达式模式匹配。现在我发现奇怪的是,如果我去掉 John 中的 n,它仍然会回响found。我认为“Joh”确实与“John”* 的模式匹配。

如果您删除“hn”并将 $name 设置为“Jo”,那么它会回显not found。它似乎只影响正则表达式模式中的最后一个字符(通配符除外)。

我正在将旧的 csh 脚本转换为 bash,但这种行为在 csh 中没有发生。是什么导致 bash 这样做?

regex bash

4
推荐指数
2
解决办法
73
查看次数