当用户从一个地方旅行到另一个地方时,我需要更新服务器的位置。我们可以在我的应用程序打开时更新。如果用户长时间没有打开app,如何更新位置?
发布于 2017-11-01 19:03:36
使用服务。
public class MyService extends Service implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "MyService";
private static final int UPDATE_INTERVAL = 5000;
private static final int FASTEST_INTERVAL = 3000;
private GoogleApiClient client;
private LocationRequest locationRequest;
private String lat, lon;
@Override
public void onConnected(@Nullable Bundle bundle) {
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(client,
locationRequest, this);
}
}else {
LocationServices.FusedLocationApi.requestLocationUpdates(client,
locationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged: " + location);
lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());
updateLocation();
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
preferences = getSharedPreferences(AppConfig.PREF, MODE_PRIVATE);
locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
client.connect();
}
@Override
public void onDestroy() //remove location update
{
Log.d(TAG, "onDestroy");
super.onDestroy();
if (client != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
client.disconnect();
}
}
private void updateLocation(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_LOCATIONUPDATE, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
}) {
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("currLat",lat );
params.put("currLon",lon );
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
}从您的活动中调用此服务。
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);https://stackoverflow.com/questions/47051255
复制相似问题