嗨,伙计们,我是这个游戏的新手,目前正试图为我的投资组合开发一个天气应用程序,但是我遇到了一些问题。显然,locationrequest方法是不推荐的,我想知道如何请求这个方法。不幸的是,我找不到任何具体的东西,因为LocationRequest.create()也不推荐.apply,而LocationRequest.Builder()则被用作替代。然而,我不知道如何从谷歌的指南。
到目前为止,这是我的代码(请尽管纠正我,我很想听到反馈):
package com.alexplas.weather
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.*
class SplashScreen : AppCompatActivity() {
lateinit var mfusedlocation:FusedLocationProviderClient
private var myRequestCode=1010
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash_screen)
mfusedlocation= LocationServices.getFusedLocationProviderClient(this)
getLastLocation()
}
@SuppressLint("MissingPermission")
private fun getLastLocation() {
if(CheckPermission()) {
if(LocationEnable()){
mfusedlocation.lastLocation.addOnCompleteListener{
task->
var location:Location?=task.result
if(location==null)
{
NewLocation()
}else{
Handler(Looper.getMainLooper()).postDelayed({
val intent= Intent(this,MainActivity::class.java)
intent.putExtra("lat",location.latitude.toString())
intent.putExtra("long",location.longitude.toString())
startActivity(intent)
finish()
},2000)
}
}
}else{
Toast.makeText(this,"Please Turn on your GPS location",Toast.LENGTH_LONG).show()
}
}else{
RequestPermission()
}
}
@SuppressLint("MissingPermission")
private fun NewLocation() {
var locationRequest=LocationRequest()
locationRequest.priority=Priority.PRIORITY_HIGH_ACCURACY
locationRequest.interval=0
locationRequest.fastestInterval=0
locationRequest.numUpdates=1
mfusedlocation=LocationServices.getFusedLocationProviderClient(this)
mfusedlocation.requestLocationUpdates(locationRequest,locationCallback, Looper.myLooper())
}
private val locationCallback=object:LocationCallback(){
override fun onLocationResult(p0: LocationResult) {
var lastLocation:Location=p0.lastLocation
}
}
private fun LocationEnable(): Boolean {
var locationManager=getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}
private fun RequestPermission() {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION),myRequestCode)
}
private fun CheckPermission(): Boolean {
if(
ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED ||
ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED
){
return true
}
return false
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if(requestCode==myRequestCode)
{
if(grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
getLastLocation()
}
}
}
}
发布于 2022-11-28 21:18:37
我已经很长时间没有使用位置服务了,所以我只是看看LocationRequest.Builder`文档并猜测您的等效代码,因为它看起来是不言自明的。构建器是一种常见的模式,与纯Kotlin API相比,在基于Java的API中使用得更多。您可以查找"java构建器模式“来阅读它。
private fun NewLocation() {
val locationRequest = LocationRequest.Builder()
.setPriority(Priority.PRIORITY_HIGH_ACCURACY)
.setIntervalMillis(0L)
.setMinUpdateIntervalMillis(0L)
.setMaxUpdates(1)
.build()
mfusedlocation = LocationServices.getFusedLocationProviderClient(this)
mfusedlocation.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}
FYI,因为你在做投资组合:
NewLocation()
重命名为类似于beginLocationRequest()
的东西。mSomething
模式(将缩写放在变量名前面)称为匈牙利符号。m
代表“成员”,但是Kotlin属性甚至不被称为成员变量。我从未见过匈牙利符号在Kotlin中使用过,也很少在Java中使用。人们普遍认为它会降低代码的可读性,特别是在现代IDE中。我建议不要在投资组合项目中使用它,因为它更有可能损害您想要给人的印象,而不是帮助您,特别是当您不一致地使用它时。https://stackoverflow.com/questions/74606139
复制相似问题