我正在尝试在后台经常进行位置检查,因此我尝试使用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);
...
}
...
}
我错过了什么吗?
答案 0 :(得分:4)
您提供给PendingIntent
的{{1}}会播放广播requestLocationUpdates()
,因为您已拨打Intent
。不过,您在PendingIntent.getBroadcast()
中提供的课程是PendingIntent
。替换Service
getService()`。