我正在尝试在后台运行位置跟踪器。为此,我读到我需要将fusedLocationProviderClient与endingIntent一起使用,以便即使最小化/关闭应用程序,我的应用程序也将继续每x秒查询用户的位置。
,我的mapActivity位于科特林。问题是,当我使用未决的Intent启动fusedLocationProviderClient时,我似乎无法触发任何位置更新(如果我使用回调在主线程中运行它,则效果很好,但是,这会使我的手机放慢速度。)
有人知道在我的情况下如何使fusedLocationProviderClient与endingIntent一起工作吗?
我尝试使用我的包名称“ com.russ.locationalarm”并在结尾附加“ .action.PROCESS_UPDATES”来处理intent.setAction()中的字符串。我是初衷,所以我不确定这是否是正确的字符串。 我也尝试过将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())
}
这是我创建我的前来意图的地方:
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)
}
这是我用来接收广播或服务的两种方法
public void onReceive(Context context, Intent intent) {
System.out.println("RECEIVED INTENT");
Log.i("asdf","ASDF ASDF ASDF");
if (intent != null) {
System.out.println("INTENT NOT EQUAL TO NULL");
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(context, locations);
Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
Log.i(TAG, Utils.getLocationUpdatesResult(context));
System.out.println("ASDF ASDF ASDF ASDF");
}
}
}
}
}
protected void onHandleIntent(Intent intent) {
System.out.println("RECEIVED INTENT");
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(this, locations);
Utils.sendNotification(this, Utils.getLocationResultTitle(this, locations));
Log.i(TAG, Utils.getLocationUpdatesResult(this));
}
}
}
}
}
我的预期结果是,我将触发onHandleIntent或onReceive方法之一。相反,在从getPendingIntent函数“获取待定意图”之后,控制台不会产生任何输出。 mLocationRequest应该每1或2秒查询一次,并且它使用循环程序和回调函数对熔融的LocationProviderClient进行了调用,因此工作更早了。