如何在服务中将位置数据保存在数组中并将该数组发送到活动/片段? [安卓/Java]

问题描述

我正在用 Java 创建一个 Android 程序,我需要每 5 秒获取一次位置(我已经使用 LocationServices 和 FusedLocationProviderClient 完成了它)。一切正常-> 我的意思是创建了日志并且正在更新位置。现在我需要将该数据(而不是在记录器中显示)保存到这样的数组中:

PSEUDOCODE:
array[0][0] = lat1,array[0][1] = lng1,array[1][0] = lat2,array[1][1] = lng2 ... etc...

并将该数组发送到我开始服务的活动/片段。怎么做?我想连续或在服务结束后传递数据。请帮帮我

我创建了 LocationService ,它正在实现这样的服务:

@Override
public void onCreate() {
    super.onCreate();
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    locationCallback = new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            Log.d("LOCALIZATOR_LOG","Lat: " + locationResult.getLastLocation().getLatitude() + ",Lng: " + locationResult.getLastLocation().getLongitude());
        }
    };
}

@Override
public int onStartCommand(Intent intent,int flags,int startId) {
    requestLocation();
    return super.onStartCommand(intent,flags,startId);
}

private void requestLocation() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(5000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback,Looper.myLooper());
}

在我的活动中,我使用这样的服务:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    if(Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
        } else {
            startLocService();
        }
    } else {
        startLocService();
    }
}

void startLocService() {
    Intent intent = new Intent(TestActivity.this,LocationService.class);
    startService(intent);
}

@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode,permissions,grantResults);
    switch(requestCode){
        case 1:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){

            } else {
                Toast.makeText(this,"I need a permission",Toast.LENGTH_LONG).show();
            }
    }
}

解决方法

您可以使用 LocalBroadcastManager 在来自 bundle 的 Intent 中通过 Service 传递数据

public void onLocationResult(LocationResult locationResult) {
     super.onLocationResult(locationResult);
     Log.d("LOCALIZATOR_LOG","Lat: " + locationResult.getLastLocation().getLatitude() + ",Lng: " + locationResult.getLastLocation().getLongitude());
     Intent intent = new Intent("LatLongUpdate");
     intent.putExtra("Latitude",locationResult.getLastLocation().getLatitude());
     intent.putExtra("Longitude",locationResult.getLastLocation().getLongitude());
     LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

然后在Activity

private ArrayList<ArrayList<Long>> locations = new ArrayList<>();
private BroadcastReceiver locationReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context,Intent intent) {
        Long latitude = intent.getLongExtra("Latitude");
        Long longitude = intent.getLongExtra("Longitude");
        //save to array in activity
        ArrayList<Long> loc = new ArrayList<>();
        loc.add(latitude);
        loc.add(longitude);
        locations.add(loc)
    }
};
LocalBroadcastManager.getInstance(this).registerReceiver(
            locationReceiver,new IntentFilter("LatLongUpdate"));