【问题标题】:Activity automatically refreshes after saving coordinates on Firebase?在 Firebase 上保存坐标后,Activity 会自动刷新吗?
【发布时间】:2022-05-20 03:33:50
【问题描述】:

在我的代码中,我使用 FusedLocationProviderClient 获取用户的位置。在回调中,我将用户的纬度和经度保存在 Firebase 实时数据库中。即使用户从一个地方移动到另一个地方,一切正常,唯一的问题是,每次它在 Firebase 上保存坐标时,活动都会刷新,我该如何停止这种自动刷新?

我只贴onCreate方法和回调方法,如果需要更多代码,我会提供。

除了每次在 Firebase 上保存新坐标时活动都会刷新之外,我的代码中一切正常吗?

注意:我的代码在片段中

private static final String TAG = "DriverMapFragment";
    int LOCATION_REQUEST_CODE = 10001;
    FusedLocationProviderClient fusedLocationProviderClient;
    LocationRequest locationRequest;
    double latitude,longitude;
    DatabaseReference databaseReference;
    String schoolName, driverPhone, vehicleNumberPlate;
    TextView newLat,newLng;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_driver_map, container, false);

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
        locationRequest = LocationRequest.create();
        locationRequest.setInterval(4000);
        locationRequest.setFastestInterval(2000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        databaseReference = FirebaseDatabase.getInstance().getReference();
        Intent intent = getActivity().getIntent();
        schoolName = intent.getStringExtra("SchoolName");
        driverPhone = intent.getStringExtra("DriverPhone");
        vehicleNumberPlate =       intent.getStringExtra("VehicleNumberPlate");
        newLat = view.findViewById(R.id.newLat);
        newLng = view.findViewById(R.id.newLng);




        return view;
    }
    
    
    
    LocationCallback locationCallback = new LocationCallback(){
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null){
                return;
            }
            for (Location location : locationResult.getLocations()){
                Log.d(TAG,"onLocationResult: " + location.toString());
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                Log.i("Lat",String.valueOf(latitude));
                Log.i("Lng",String.valueOf(longitude));
                


                LocationHelper helper = new LocationHelper(
                        location.getLongitude(),
                        location.getLatitude()
                );

                FirebaseDatabase.getInstance().getReference().child("Schools")
                        .child(schoolName)
                        .child("drivers")
                        .child(vehicleNumberPlate)
                        .child("driverLocation")
                        .setValue(helper).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        if(task.isSuccessful()){
                            Log.i("Success","Location Saved");
                        }
                        else{
                            Log.i("Failure","Location Not Saved");
                        }
                    }
                });



            }
        }
    };

【问题讨论】:

  • 是否有一部分代码是您从 firebase 读取 latlng 的?
  • 不,我的代码只是将 latlng 存储在 firebase 上,我有另一个应用程序可以从 firebase 获取 latlng

标签: android android-fragments firebase-realtime-database fusedlocationproviderclient


【解决方案1】:

确保你使用addListenerForSingleValueEvent而不是addValueEventListener,否则当你将数据更新到Firebase时,addValueEventListener将被再次调用并运行你调用的Activity。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多