gpt4 book ai didi

android - 如何在Kotlin中设置从服务到 Activity 的回调?

转载 作者:行者123 更新时间:2023-11-29 14:16:29 25 4
gpt4 key购买 nike

我是Kotlin的新手,我每次通过fn_update调用服务中的方法时,都会尝试将服务中的位置数据传递给我的活动。

这是我的服务

class LocationService : Service(), LocationListener {
private var context: Context? = null
internal var isGPSEnable = false
internal var isNetworkEnable = false
internal var latitude: Double = 0.toDouble()
internal var longitude: Double = 0.toDouble()
internal var locationManager: LocationManager? = null
internal var location: Location? = null
private val mHandler = Handler()
private var mTimer: Timer? = null
internal var notify_interval: Long = 3000
var track_lat = 0.0
var track_lng = 0.0
internal lateinit var intent: Intent
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
mTimer = Timer()
mTimer!!.schedule(TimerTaskToGetLocation(), 5, notify_interval)
intent = Intent(str_receiver)
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
this.context = this
return Service.START_NOT_STICKY
}

override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "onDestroy <<")
if (mTimer != null) {
mTimer!!.cancel()
}
}
private fun trackLocation() {
Log.e(TAG, "trackLocation")
val TAG_TRACK_LOCATION = "trackLocation"
val params = HashMap<String, String>()
params["latitude"] = "" + track_lat
params["longitude"] = "" + track_lng
}

override fun onLocationChanged(location: Location) {

}

override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {

}

override fun onProviderEnabled(provider: String) {

}

override fun onProviderDisabled(provider: String) {

}

private fun fn_getlocation() {
locationManager = applicationContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
isGPSEnable = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
isNetworkEnable = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
if (!isGPSEnable && !isNetworkEnable) {
Log.e(TAG, "CAN'T GET LOCATION")
stopSelf()
} else {
if (isNetworkEnable) {
location = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
}
}
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (location != null) {
Log.e(TAG, "isNetworkEnable latitude" + location!!.latitude + "\nlongitude" + location!!.longitude + "")
latitude = location!!.latitude
longitude = location!!.longitude
track_lat = latitude
track_lng = longitude
fn_update(location!!);
}
}
} else if (isGPSEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (location != null) {
Log.e(TAG, "isGPSEnable latitude" + location!!.latitude + "\nlongitude" + location!!.longitude + "")
latitude = location!!.latitude
longitude = location!!.longitude
track_lat = latitude
track_lng = longitude
fn_update(location!!);
}
}
}
trackLocation()
}
}

private inner class TimerTaskToGetLocation : TimerTask() {
override fun run() {
mHandler.post { fn_getlocation() }
}
}}


这是我的活动

  @RequiresApi(api = Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val background = Intent(this, LocationService::class.java)

this.startService(background)


请帮助我如何从服务中回调并将数据传递给活动。

提前致谢

最佳答案

有很多文章如何做到这一点。

例如,您可以检查此答案-https://stackoverflow.com/a/7283298/9522749

关于android - 如何在Kotlin中设置从服务到 Activity 的回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58624961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com