Rus*_*ler 5 android-intent android-pendingintent fusedlocationproviderapi location-updates fusedlocationproviderclient
我正在尝试在后台运行位置跟踪器。为此,我读到我需要将endingIntent与fusedLocationProviderClient一起使用,以便我的应用程序将继续每x秒查询用户的位置,即使应用程序被最小化/关闭。
我的 mapActivity 在 kotlin 中。问题是,当我使用pendingIntent启动fusedLocationProviderClient时,我似乎无法触发任何位置更新(如果我使用回调在主线程中运行它,它会很好地工作,但是,这会减慢我的手机速度)。
有谁知道如何让 fusedLocationProviderClient 在我的情况下与未决意图一起工作?
我尝试使用intent.setAction() 中的字符串,使用我的包名称“com.russ.locationalarm”并在末尾附加“.action.PROCESS_UPDATES”。我对意图不熟悉,所以我不确定这是否是正确使用的字符串。我也尝试过将 PendingIntent.GetService() 切换为 PendingIntent.getBroadCast(),但似乎都不起作用。
这是我启动 fusedLocationProviderClient 的函数:
private fun startLocationUpdates() {
// Create the location request to start receiving updates
mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mLocationRequest!!.setInterval(INTERVAL)
mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)
// Create LocationSettingsRequest object using location request
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest!!)
val locationSettingsRequest = builder.build()
val settingsClient = LocationServices.getSettingsClient(this)
settingsClient.checkLocationSettings(locationSettingsRequest)
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
}
// mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
println("requesting location updates")
Utils.setRequestingLocationUpdates(this,true)
mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, getPendingIntent())
}
Run Code Online (Sandbox Code Playgroud)
这是我创建待决意图的地方:
private fun getPendingIntent(): PendingIntent {
// Intent intent = new Intent(this, LocationUpdatesIntentService.class);
// intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
// return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
println("getting pending intent")
var intent = Intent(this,LocationUpdatesIntentService::class.java)
intent.setAction("com.russ.locationalarm.action.PROCESS_UPDATES")
return PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
}
Run Code Online (Sandbox Code Playgroud)
这是我用来接收广播或服务的两种方法
private fun startLocationUpdates() {
// Create the location request to start receiving updates
mLocationRequest!!.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mLocationRequest!!.setInterval(INTERVAL)
mLocationRequest!!.setFastestInterval(FASTEST_INTERVAL)
// Create LocationSettingsRequest object using location request
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest!!)
val locationSettingsRequest = builder.build()
val settingsClient = LocationServices.getSettingsClient(this)
settingsClient.checkLocationSettings(locationSettingsRequest)
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
// new Google API SDK v11 uses getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return
}
// mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
println("requesting location updates")
Utils.setRequestingLocationUpdates(this,true)
mFusedLocationProviderClient!!.requestLocationUpdates(mLocationRequest, getPendingIntent())
}
Run Code Online (Sandbox Code Playgroud)
我的预期结果是我将触发 onHandleIntent 或 onReceive 方法之一。相反,在从 getPendingIntent 函数“获取待处理意图”后,控制台不会产生任何输出。mLocationRequest 应该每 1 或 2 秒查询一次,并且它之前使用 Looper 和回调调用 fusedLocationProviderClient 来工作,所以我知道它不是那样。
| 归档时间: |
|
| 查看次数: |
1265 次 |
| 最近记录: |