Firebase的位置更新太快,滞后于应用

时间:2016-07-19 01:06:11

标签: java android firebase firebase-realtime-database

我目前的目标是将LatLng发送到Firebase。我确实做到了这一点,但我现在遇到的问题是,在对Firebase进行了几次更新之后,我的位置字段开始超快速地接收更新,这反过来又滞后于我的应用程序并且还增加了我的手机温度。

如何在没有应用程序延迟的情况下每分钟两次向firebase发送位置更新?

我使用onLocationChanged(Location)方法将位置更新发送到Firebase。

这是我的Mapfragment类。

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.mycompany.neighbors.MainActivity;
import com.mycompany.neighbors.R;
import com.mycompany.neighbors.User;

/**
 * Created by joshua on 5/25/2016.
 */
public class MapFragment extends Fragment implements OnMapReadyCallback,LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

    private GoogleApiClient mGoogleApiClient;
    private final String FIREBASE_URL = "MY_URL";
    private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 101;

    SupportMapFragment mSupportMapFragment;
    private GoogleMap maps;
    private boolean permissionIsGranted = false;

    private LatLng mLatLng;

    private User mApplicationUser;
    private static String mApplicationUserUID;


    public static MapFragment newInstance(int index){
        MapFragment mapFragment = new MapFragment();
        Bundle args = new Bundle();
        args.putInt("index",index);
        mapFragment.setArguments(args);
        return mapFragment;

    }

    private void createMap(){
        mSupportMapFragment = SupportMapFragment.newInstance();
        FragmentManager fm = getFragmentManager();
        mSupportMapFragment.getMapAsync(this);
        if(!mSupportMapFragment.isAdded())
            fm.beginTransaction().add(R.id.map_frag,mSupportMapFragment).commit();

        else if(mSupportMapFragment.isAdded())
            fm.beginTransaction().hide(mSupportMapFragment).commit();
        else
            fm.beginTransaction().show(mSupportMapFragment).commit();

    }

    private void requestLocationUpdates() {
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(60000);

        if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_FINE_LOCATION);
            }
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

/////////////////////////////////////////OVERRIDE METHODS////////////////////////////////////////////////////////////
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        View v = inflater.inflate(R.layout.fragment_map,container,false);
        mApplicationUserUID = MainActivity.getUID();

        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        createMap();
        return v;

    }
////////////////////////////////////////LIFECYCLE METHODS///////////////////////////////////////////////////////////
    @Override
    public void onStart(){
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    public void onResume(){
        super.onResume();
        if(permissionIsGranted){
            if(mGoogleApiClient.isConnected()){
                requestLocationUpdates();

            }
        }
    }

    @Override
    public void onStop(){
        if(permissionIsGranted){
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }
///////////////////////LIFECYCLE METHODS//////////////////////////////////////////////

    @Override
    public void onMapReady(GoogleMap googleMap) {
        maps = googleMap;

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d("TAG_JOSH", "onConnected");

        requestLocationUpdates();

    }

    @Override
    public void onConnectionSuspended(int i) {

        Log.d("TAG_JOSH", "Connection suspended");

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        Log.d("TAG_JOSH", "Connection failed");

    }

    @Override
    public void onLocationChanged(final Location location) {

        Log.d("TAG_JOSH","Latitude: " +Double.toString(location.getLatitude()));
        final LatLng coordinates = new LatLng(location.getLatitude(),location.getLongitude());
        final Firebase userRef = new Firebase("MY_URL");

        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userRef.setValue(coordinates);


            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){

        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
        switch(requestCode){
            case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    //Permission granted
                    permissionIsGranted = true;

                } else{
                    //Permission denied
                    permissionIsGranted = false;
                    Toast.makeText(getContext(),"This app requires location permissions", Toast.LENGTH_SHORT).show();
                }
                break;

        }

    }


/////////////////////////////////////////OVERRIDE METHODS////////////////////////////////////////////////////////////



}

1 个答案:

答案 0 :(得分:0)

如果您尝试每分钟发送一次您的位置,则需要删除FirebaseonLocationChanged内发送位置更新的部分内容。

添加CountDownTimer并触发,以便在每30秒过后发送一次位置更新。

所以你需要做这样的事情。

取两个全局变量

private Location currentLocation;
private Location previousLocation; 

现在你的onCreateView内部会声明一个像这样的计时器。

new CountDownTimer(30000, 1000) { // 30 seconds timer

    public void onTick(long millisUntilFinished) {
        // Do nothing
    }

    public void onFinish() {
        // Send location update to firebase here if the previous location sent to server and the current location is not the same. 

        final LatLng coordinates = new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude());

        if(currentLocation == previousLocation) return;  // Pseudo code

        final Firebase userRef = new Firebase("MY_URL");

        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userRef.setValue(coordinates);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        // Now start the timer from here again.
        this.start();     // Pseudo code. 
    }

}.start();

onLocationChanged功能中,只需像这样更新currentLocation

@Override
public void onLocationChanged(final Location location) {
    currentLocation = location; // Update the current location here
}