首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在应用程序未打开时将位置更新到服务器

如何在应用程序未打开时将位置更新到服务器
EN

Stack Overflow用户
提问于 2017-11-01 16:56:47
回答 1查看 71关注 0票数 0

当用户从一个地方旅行到另一个地方时,我需要更新服务器的位置。我们可以在我的应用程序打开时更新。如果用户长时间没有打开app,如何更新位置?

EN

回答 1

Stack Overflow用户

发布于 2017-11-01 19:03:36

使用服务。

代码语言:javascript
复制
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);
}
}

从您的活动中调用此服务。

代码语言:javascript
复制
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47051255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档