[英]Android requestLocationUpdates using PendingIntent with BroadcastReceiver
[英]RequestLocationUpdates with PendingIntent never fires
我试图在后台经常执行位置检查,所以我试图使用IntentService从FusedLocationApi获取位置更新。 但是PendingIntent永远不会触发。
码:
public class LocationIntentService extends IntentService
implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{
private static GoogleApiClient mApiClient;
...
@Override
public void onCreate() {
if (mApiClient == null) {
mApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mApiClient.connect();
}
}
...
@Override
public void onConnected(Bundle bundle) {
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
locationRequest.setInterval(Constants.LOCATION_UPDATE_INTERVAL_MILIS);
locationRequest.setFastestInterval(Constants.LOCATION_UPDATE_INTERVAL_MILIS / 2);
Intent locationIntent = new Intent(getApplicationContext(), LocationIntentService.class);
PendingIntent locationPendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
0,
locationIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
LocationServices.FusedLocationApi
.requestLocationUpdates(mApiClient, locationRequest,locationPendingIntent);
...
}
...
}
我想念什么吗?
您提供给requestLocationUpdates()
的PendingIntent
将触发广播Intent
因为您已调用PendingIntent.getBroadcast()
。 但是,您在PendingIntent
提供的类是Service
。 将getBroadcast() with
替换getBroadcast() with
getService()`。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.