带有 PendingIntent 的 RequestLocationUpdates 永远不会触发
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
中提供的 class 是 Service
。替换 getBroadcast() with
getService()`.
我正在尝试 运行 经常在后台检查位置,因此我正在尝试使用 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
中提供的 class 是 Service
。替换 getBroadcast() with
getService()`.