由于我在我的位置使用googlemap,每两分钟我想要更新位置,有时onlocationchanged方法不会在我的应用程序中调用。 请帮我解决我的问题。
编码:
public class MyLocationService1 extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {
LocationManager locationManager ;
SharedPreferences sharedPreferences,startLesson_preference;
SharedPreferences.Editor editor;
String gs_var_userid,gs_var_usetoken;
private GoogleApiClient mGoogleApiClient = null;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(5000)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
public static boolean isGPSEnabled = false;
public static boolean isNetworkEnabled = false;
private final int UPDATE_INTERVAL = 5000; // 5 sec
private final int FASTEST_INTERVAL = 5000; // 1 sec
private final int DISPLACEMENT = 0; // 10 meters
LocationRequest mLocationRequest;
public void createLocationRequest() {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
String provider;
//to check gps is enabled or not
@Override
public void onCreate() {
super.onCreate();
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Log.i("FG","onstartcommand");
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
createLocationRequest();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
private synchronized void buildGoogleApiClient() {
try {
mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
try {
Log.i("FG","----onConnected----service1");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(3000);
locationRequest.setFastestInterval(3000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, locationRequest, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("AA","--location : "+location.getLatitude()+","+location.getLongitude());
}
});
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.i("AA","--LAT & LONG : "+mLastLocation.getLatitude()+","+mLastLocation.getLongitude());
}
else
{
buildGoogleApiClient();
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
}
// to get current latitude and longitude
// storing the updated latitde and longitude when user moving and reach destination
@Override
public void onLocationChanged(Location location) {
try {
Log.i("MS","service class********"+location);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
注意:Onconnected方法正在执行,lastknownlocation也显示但是onlocationchanged方法没有执行。在我的项目中每隔2分钟计算一次距离非常重要。请帮帮我