0
ユーザーがある場所から別の場所に移動したときにサーバーの場所を更新する必要があります。私たちのアプリが開くときに更新することができます。ユーザーが長い時間アプリを開いていない場合でも、場所を更新するにはどうすればよいですか?アプリケーションが開いていなくてもサーバーへの場所の更新方法
ユーザーがある場所から別の場所に移動したときにサーバーの場所を更新する必要があります。私たちのアプリが開くときに更新することができます。ユーザーが長い時間アプリを開いていない場合でも、場所を更新するにはどうすればよいですか?アプリケーションが開いていなくてもサーバーへの場所の更新方法
サービスをご利用ください。
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);
ロケーションクラスとともにGCMネットワークマネージャまたはFirebaseジョブスケジューラを使用してみます。 – param