您好,我在应用程序中使用的是Mapbox,但是从我阅读的Mapbox来看,该位置仅在应用程序中而不是在后台。
locationComponent.getLastKnownLocation () // to get location with mapbox when in application
我有一个Foregorund服务,该服务在后台使用该位置,因此我制作了一个将位置返回给我的类,一切正常。 我的问题是用户是否在应用程序中,既mapbox更新位置,又我的对象频繁更新位置...在这种情况下,应用程序不会消耗太多内存? 有没有一种方法可以只在其中一个对象上占据位置?
我的位置类别:
package com.example.trackingapp;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Looper;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.firebase.firestore.GeoPoint;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LocationUpdater {
private Context context;
private FusedLocationProviderClient mFusedLocationClient;
private static Location lastLocation;
private static final long UPDATE_INTERVAL = 10000, FASTEST_INTERVAL = 5000; // = 5 seconds
LocationUpdater(Context context) {
this.context = context;
requestLocation();
}
private void requestLocation() {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
LocationRequest locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_INTERVAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
}
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper());
}
@SuppressLint("MissingPermission")
public static Location getLocation() {
return lastLocation;
}
private LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
lastLocation = locationList.get(locationList.size() - 1);
}
}
};
public void stopLocationUpdates(){
if (mFusedLocationClient != null) {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
}
@SuppressLint("MissingPermission")
public static Map<String, Object> getHash() {
Log.d("Location"," " +lastLocation.getLatitude()+""+ lastLocation.getLongitude());
Map<String,Object> userLocation = new HashMap<>();
userLocation.put("userLocation",new GeoPoint(lastLocation.getLatitude(), lastLocation.getLongitude()));
return userLocation;
}
}
我在后台服务中的位置-代码仍在实现中:
private Runnable mToastRunnable = new Runnable() {
@Override
public void run() {
IsServiceWorking.isWorking=true;
Log.d("time-",""+time);
if(time!=0){
date= new Date();
systemTime =date.getTime();
Log.d("timesystem-",""+systemTime);
if(systemTime<time){
wrData.setUserLocation(LocationUpdater.getHash());
Log.i("KO","dodododo");
}else {
wrData.setUserLocation(LocationUpdater.getHash());
Log.i("KDO","KDOKDO");
}
}
mHandler.postDelayed(this, 11000);
}
};