不说谎,我在科特林是个新手,所以我需要分析完整的代码。
在启动时,它请求权限(不再需要,这只是测试项目),按下按钮应该将textView更改为MyLocation
这是代码:
class MainActivity : AppCompatActivity() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private lateinit var text: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text = findViewById(R.id.textView)
val button: Button = findViewById(R.id.button)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
val requestMultiplePermissions = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach {
Log.e("DEBUG", "${it.key} = ${it.value}")
}
}
button.setOnClickListener {
getLastKnownLocation()
}
requestMultiplePermissions.launch(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
)
}
private fun getLastKnownLocation() {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
text.text = "do not grants the permission"
return
}
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
text.text = "My location " + location.latitude.toString() + ", " + location.longitude.toString()
}
else{
text.text = "location = null"
}
}
} }
//added to Manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
但是它写了"location = null",这里有问题吗?
它不要求使用模拟器的权限,我使用连接的设备进行测试
逻辑猫:
2021-10-27 14:51:55.818 10025-10025/? I/ample.idssende: Late-enabling -Xcheck:jni
2021-10-27 14:51:55.818 10025-10025/? I/ample.idssende: Late-enabling -Xcheck:jni
2021-10-27 14:51:55.818 10025-10025/? I/ample.idssende: Late-enabling -Xcheck:jni
2021-10-27 14:51:56.009 10025-10025/? E/libc: Access denied finding property "persist.vendor.sys.activitylog"
2021-10-27 14:51:56.004 10025-10025/? W/re-initialized>: type=1400 audit(0.0:35158): avc: denied { read } for name="u:object_r:mtk_amslog_prop:s0" dev="tmpfs" ino=7714 scontext=u:r:untrusted_app:s0:c188,c256,c512,c768 tcontext=u:object_r:mtk_amslog_prop:s0 tclass=file permissive=0
2021-10-27 14:51:56.009 10025-10025/? E/libc: Access denied finding property "persist.vendor.sys.activitylog"
2021-10-27 14:51:55.924 16571-16610/? I/MicroDetectionState: Should stop hotword detection immediately - false
2021-10-27 14:51:56.004 10025-10025/? W/re-initialized>: type=1400 audit(0.0:35158): avc: denied { read } for name="u:object_r:mtk_amslog_prop:s0" dev="tmpfs" ino=7714 scontext=u:r:untrusted_app:s0:c188,c256,c512,c768 tcontext=u:object_r:mtk_amslog_prop:s0 tclass=file permissive=0
2021-10-27 14:51:56.009 10025-10025/? E/libc: Access denied finding property "persist.vendor.sys.activitylog"
2021-10-27 14:51:56.004 10025-10025/? W/re-initialized>: type=1400 audit(0.0:35158): avc: denied { read } for name="u:object_r:mtk_amslog_prop:s0" dev="tmpfs" ino=7714 scontext=u:r:untrusted_app:s0:c188,c256,c512,c768 tcontext=u:object_r:mtk_amslog_prop:s0 tclass=file permissive=0
2021-10-27 14:51:57.321 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2021-10-27 14:51:57.321 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2021-10-27 14:51:57.321 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2021-10-27 14:51:57.321 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2021-10-27 14:51:57.321 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2021-10-27 14:51:57.452 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden field Landroid/view/WindowInsets;->CONSUMED:Landroid/view/WindowInsets; (light greylist, reflection)
2021-10-27 14:51:57.483 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden method Landroid/view/View;->getAccessibilityDelegate()Landroid/view/View$AccessibilityDelegate; (light greylist, linking)
2021-10-27 14:51:57.637 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2021-10-27 14:51:57.640 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2021-10-27 14:51:58.209 10025-10025/com.example.idssender D/OpenGLRenderer: Skia GL Pipeline
2021-10-27 14:51:58.211 10025-10025/com.example.idssender I/SurfaceFactory: [static] sSurfaceFactory = com.mediatek.view.impl.SurfaceFactoryImpl@7ee86fd
2021-10-27 14:51:58.237 10025-10025/com.example.idssender D/ViewRootImpl[MainActivity]: hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2021-10-27 14:51:58.248 10025-10025/com.example.idssender V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@22195f9, this = DecorView@b94e93e[MainActivity]
2021-10-27 14:51:58.254 10025-10025/com.example.idssender E/DEBUG: android.permission.ACCESS_FINE_LOCATION = true
2021-10-27 14:51:58.254 10025-10025/com.example.idssender E/DEBUG: android.permission.ACCESS_COARSE_LOCATION = true
2021-10-27 14:51:58.315 10025-10025/com.example.idssender W/ample.idssende: Accessing hidden method Landroid/graphics/Insets;->of(IIII)Landroid/graphics/Insets; (light greylist, linking)
2021-10-27 14:51:58.357 10025-10025/com.example.idssender D/Surface: Surface::allocateBuffers(this=0x78180db000)
2021-10-27 14:51:58.382 10025-10127/com.example.idssender I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2021-10-27 14:51:58.382 10025-10127/com.example.idssender I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2021-10-27 14:51:58.382 10025-10127/com.example.idssender I/OpenGLRenderer: Initialized EGL, version 1.4
2021-10-27 14:51:58.383 10025-10127/com.example.idssender D/OpenGLRenderer: Swap behavior 2
2021-10-27 14:51:58.383 10025-10127/com.example.idssender E/OpenGLRenderer: Device claims wide gamut support, cannot find matching config, error = EGL_SUCCESS
2021-10-27 14:51:58.398 10025-10127/com.example.idssender D/Surface: Surface::connect(this=0x78180db000,api=1)
2021-10-27 14:51:58.402 10025-10127/com.example.idssender D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2021-10-27 14:51:58.529 10025-10127/com.example.idssender E/ion: ioctl c0044901 failed with code -1: Invalid argument
发布于 2021-10-27 11:57:43
您的代码没有问题,应该可以正常工作。
要使工作,只需打开谷歌地图应用程序在您的设备(真实或模拟器),并点击我的位置按钮。
有时,如果没有缓存位置,则设备的最后一个位置为null。按照上面的步骤,将确保最后一个已知的位置不是空的。重新安装应用程序,它应该显示设备上一个已知位置的纬度和经度。
NB -您不需要同时请求ACCESS_COARSE_LOCATION
和ACCESS_FINE_LOCATION
ACCESS_FINE_LOCATION
权限包括ACCESS_FINE_LOCATION
和GPS_PROVIDER的权限,因此只需要请求ACCESS_FINE_LOCATION
权限。
https://stackoverflow.com/questions/69736761
复制相似问题