我想在用户点击按钮时向Firebase发送实时位置更新

时间:2017-07-04 08:06:15

标签: android firebase android-location geofire

我想在用户点击按钮时向Firebase发送位置更新。更新设置为每3秒发送一次位置更改,当我使用模拟器时它会向firebase发送详细信息,但是当我使用自己的设备时它不会发送。 它不会在每3秒后更新一次。 请帮帮我是否需要使用geofire(如何使用)?因为我是这个新手

这是我的代码

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;
    }


}
}

0 个答案:

没有答案