我正在使用创建背景位置跟踪器Google FusedLocation API
。在requestLocationUpdates()
,我PendingIntent
用于后台操作。当我运行我的应用程序时,前台位置跟踪器工作正常。但是,我的IntentService
课似乎不起作用。我没有收到任何通知,后台没有任何反应。这就是我Google FusedLocation API
在主片段类中实现的方式:
@Override
public void onConnected(Bundle bundle) {
.......
//Starting Background Location Tracker
if (mRequestingLocationUpdates)
{
createLocationRequest();
startLocationUpdates();
}
}
protected void startLocationUpdates() {
locationIntent = new Intent(getActivity().getApplicationContext(), LocationHandlerService.class);
locationPendingIntent = PendingIntent.getService(getActivity().getApplicationContext(), 5,
locationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, locationPendingIntent);
}
这是我的LocationHandlerService
课:
public class LocationHandlerService extends IntentService {
Location mLocation;
NotificationManager notificationManager;
public LocationHandlerService() {
super("Services.LocationHandlerService");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
protected void onHandleIntent(Intent intent) {
if (LocationResult.hasResult(intent)) {
LocationResult locationResult = LocationResult.extractResult(intent);
mLocation = locationResult.getLastLocation();
if (mLocation != null) {
setupNotification(mLocation);
}
}
}
//Setup Notification
private void setupNotification(Location location) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.location_start_notification)
.setOngoing(true)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle(getResources().getString(R.string.location_notification))
.setContentText("Lat: " + location.getLatitude() + ", Long: " + location.getLongitude());
notificationManager.notify(0, notification.build());
}
}
谁能告诉我的代码有什么问题???