I want to send an real time location updates to Firebase when the user Clicks on a button

不羁的心 提交于 2019-12-25 18:16:06

问题


I want to send an location updates to Firebase when the user Clicks on a button. Updates are set to send every 3 seconds on location change, when I use an emulator it sends details to firebase, but when I use my own device it does not send. It does not update after every 3 seconds. Please help me is there need to use geofire (how to use)? As I am new to this

Here is my code

import android.Manifest;
import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class gpdfuel extends AppCompatActivity {

private Button btn;
private Button start;
private FirebaseAuth fb;
private FirebaseAuth.AuthStateListener fbListener;
private DatabaseReference databaseReference;
private String userID;
LocationManager loc;
LocationListener lic;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gpdfuel);
    start = (Button) findViewById(R.id.start);

    loc = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

    fb = FirebaseAuth.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    FirebaseUser user = fb.getCurrentUser();

    btn = (Button) findViewById(R.id.lout);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(loc != null){
                //noinspection MissingPermission
                loc.removeUpdates(lic);
            }
            Intent a = new Intent(gpdfuel.this,login.class);
            startActivity(a);
        }
    });

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            loc.requestLocationUpdates("gps", 5000, 2, lic);
        }
    });

    lic = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            FirebaseUser user = fb.getCurrentUser();
            databaseReference.child(user.getUid()).push().setValue(location.getLatitude() + ",   " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.INTERNET
        },10);
        return;
    }else{
        configureButton();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case 10:
            configureButton();
            break;
        default:
            break;
    }
}

void configureButton(){
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
                    ,10);
        }
        return;
    }


}
}

来源:https://stackoverflow.com/questions/44900352/i-want-to-send-an-real-time-location-updates-to-firebase-when-the-user-clicks-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!