firebase不会在后台运行

时间:2018-03-30 17:35:09

标签: android firebase firebase-realtime-database

我正在开发一个Android应用程序来跟踪后台的用户位置并继续将其更新到firebase数据库,因为位置更改应用程序打开时应用程序正常工作但是当我关闭它时,firebase停止设置新值,它将在我再次打开应用程序后直接设置新值

这是我的android服务代码:

public class LocationServiceFB extends Service implements LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private Location mLastLocation;
DatabaseReference instructors;
GeoFire geofire;
String UserId;
SharedPreferences pref;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL = 5000;
private static  int DISPLACEMENT = 10;
private  static int FATEST_INTERVAL = 3000;
private LocationManager mLocationManager;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not Yet Implemented");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    instructors = FirebaseDatabase.getInstance().getReference("OnlineInstructors");
    geofire = new GeoFire(instructors);
    pref = this.getSharedPreferences("LoginTrack", Context.MODE_PRIVATE);
    UserId = pref.getString("firebasekey","");

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

    return START_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    instructors.child(UserId).removeValue();
}




@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();

}

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

}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    geofire.setLocation(UserId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
        @Override
        public void onComplete(String key, DatabaseError error) {
            Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );

        }
    });
}




public void uploadLocation(){

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        Log.d("EER","np permissions");
        return;

    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    final double latitude = mLastLocation.getLatitude();
    final double longitude = mLastLocation.getLongitude();
    geofire.setLocation(UserId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
        @Override
        public void onComplete(String key, DatabaseError error) {
            Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );

        }
    });
}



}

2 个答案:

答案 0 :(得分:0)

Android可能会杀死您应用的流程,转而支持更重要的应用。当前景不再可见时,Android也可能会阻止您的网络进程。这是为了防止行为不当的应用程序消耗太多资源。

如果您的应用需要在用户不再使用时继续联网,您必须启动前台服务,这也要求您向用户显示通知,告知您的应用仍在运行。< / p>

答案 1 :(得分:0)

您正在使用绑定服务将您的职位升级到服务器。 您的服务会随应用而被杀死。 使用未绑定的服务。即使应用程序被终止,未绑定服务仍在后台运行。因此,即使在具有未绑定服务的后台,这也可以正常工作。