我已编写服务以在我的应用中提供位置更新。问题是位置服务不提供位置更新,除非位置已更改。我正在使用模拟器来测试它。以下是代码:
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
基于this回答。 setInterval方法应该定期更新。
这里有什么问题?
完整代码:
package com.followme;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Set;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, LocationListener, GoogleApiClient.OnConnectionFailedListener {
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
private static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private static final int MY_PERMISSIONS_REQUEST_LOCATION = 1;
;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
public void onCreate() {
super.onCreate();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Log.i("LOC_SERVICE", "onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("LOC_SERVICE", "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LOC_SERVICE", "onStartCommand");
super.onStartCommand(intent,flags,startId);
mGoogleApiClient.connect();
return START_STICKY;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.i("LOC_SERVICE", "onConnected");
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("Location service","conenction failed");
}
@Override
public void onLocationChanged(Location location) {
Log.i("LOC_SERVICE", "onLocationChanged");
final Set<String> senderList = AppUtil.getSendersList(this);
try {
final JSONObject msg = new JSONObject().put("type", "loc").put("lat", location.getLatitude()).put("lng", location.getLongitude()).put("fcm","\""+AppUtil.getFCM(this)+"\"");
// final String[] fcmArr = senderList.toArray(new String[senderList.size()]);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
for(String fcm:senderList) {
try {
FCMServer.sendMsg(msg, fcm);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}}