Android:Firebase更新问题当应用程序处于后台时

嘿我要开发一个位置跟踪器应用程序,其中,此应用程序在客户端设备中不断将其位置发送到firebase数据库。 这里的问题是它只会在前3分钟将数据发送到firebase,然后就不会了。 我不知道发生了什么事。 ? 为此,即使我发了一条日志消息,即使在三分钟后也能完美地打印日志消息。任何人请帮助这个……..! 这里我附上了3个文件One BackgroundLocation:后台的服务是提取设备位置并调用LocationReceiver,它扩展了广播接收器,它将打印日志消息并通过FBSender将数据发送到firebase。

提前致谢

BackgroundLocation.java在后台运行以获取位置详细信息并调用广播Reveiver。 LocationReveiver.java

/** * Created by geekyint on 1/7/16. */ public class BackgroundLocation extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { IBinder mBinder = new LocalBinder(); private GoogleApiClient mGoogleApiClient; private PowerManager.WakeLock mWakeLock; private LocationRequest mlocationRequest; //Flag for boolean request private boolean mInProgress; private boolean serviceAvailabe = false; public class LocalBinder extends Binder { public BackgroundLocation getServerInstance() { return BackgroundLocation.this; } } @Override public void onCreate() { super.onCreate(); mInProgress = false; //Create the lcoation request object mlocationRequest = LocationRequest.create(); //Use the acurecy mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //The INTERVAL mlocationRequest.setInterval(Constants.UPDATE_INTERVAL); //The FAST INTERVAL mlocationRequest.setFastestInterval(Constants.FAST_INTERVAL); serviceAvailabe = serviceConnected(); setUpALocationClientIfNeeded(); ComponentName receiver = new ComponentName(this, LocationReceiver.class); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); /*ComponentName receiver1 = new ComponentName(this, FireBaseSender.class); PackageManager pm1 = this.getPackageManager(); pm1.setComponentEnabledSetting(receiver1, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);*/ } private void setUpALocationClientIfNeeded() { if (mGoogleApiClient == null) { buildGoogleApiClient(); } } //Create the new Connection to the client private void buildGoogleApiClient() { this.mGoogleApiClient = new GoogleApiClient.Builder(this) .addOnConnectionFailedListener(this) .addConnectionCallbacks(this) .addApi(LocationServices.API) .build(); } private boolean serviceConnected() { //Check the google Play service availibility int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //IF AVAILABLE if (resultCode == ConnectionResult.SUCCESS) { return true; } else { return false; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE); if (this.mWakeLock == null) { this.mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); } if (!this.mWakeLock.isHeld()) { this.mWakeLock.acquire(); } if (!serviceAvailabe || mGoogleApiClient.isConnected() || mInProgress) { return START_STICKY; } setUpALocationClientIfNeeded(); if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() || !mInProgress) { // appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Started", Constants.LOG_FILE); mInProgress = true; mGoogleApiClient.connect(); } return START_STICKY; } @Override public void onLocationChanged(Location location) { String msg = Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()); Log.d("debug", msg); // Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); // appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ":" + msg, Constants.LOCATION_FILE); } @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } public String getTime() { SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return mDateFormat.format(new Date()); } public void appendLog(String text, String filename) { File logFile = new File(filename); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { //BufferedWriter for performance, true to set append to file flag BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); buf.append(text); buf.newLine(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onDestroy() { // Turn off the request flag this.mInProgress = false; if (this.serviceAvailabe && this.mGoogleApiClient != null) { this.mGoogleApiClient.unregisterConnectionCallbacks(this); this.mGoogleApiClient.unregisterConnectionFailedListener(this); this.mGoogleApiClient.disconnect(); // Destroy the current location client this.mGoogleApiClient = null; } // Display the connection status // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": // Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); if (this.mWakeLock != null) { this.mWakeLock.release(); this.mWakeLock = null; } // appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Stopped", Constants.LOG_FILE); ComponentName receiver = new ComponentName(this, LocationReceiver.class); PackageManager pm = this.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); /* ComponentName receiver1 = new ComponentName(this, FireBaseSender.class); PackageManager pm1 = this.getPackageManager(); pm1.setComponentEnabledSetting(receiver1, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);*/ super.onDestroy(); } /* * Called by Location Services when the request to connect the * client finishes successfully. At this point, you can * request the current location or start periodic updates */ @Override public void onConnected(Bundle bundle) { // Request location updates using static settings if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } Intent intent = new Intent (this, LocationReceiver.class); PendingIntent pendingIntent = PendingIntent .getBroadcast(this, 54321, intent, PendingIntent.FLAG_CANCEL_CURRENT); LocationServices.FusedLocationApi.requestLocationUpdates(this.mGoogleApiClient, mlocationRequest, pendingIntent); } /* * Called by Location Services if the connection to the * location client drops because of an error. */ @Override public void onConnectionSuspended(int i) { // Turn off the request flag mInProgress = false; // Destroy the current location client mGoogleApiClient = null; // Display the connection status // Toast.makeText(this, DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected. Please re-connect.", Toast.LENGTH_SHORT).show(); // appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Disconnected", Constants.LOG_FILE); } /* * Called by Location Services if the attempt to * Location Services fails. */ @Override public void onConnectionFailed(ConnectionResult connectionResult) { mInProgress = false; /* * Google Play services can resolve some errors it detects. * If the error has a resolution, try sending an Intent to * start a Google Play services activity that can resolve * error. */ if (connectionResult.hasResolution()) { // If no resolution is available, display an error dialog } else { } } 

}

这里的LocationReceiver代码:

 public class LocationReceiver extends BroadcastReceiver { private String TAG = this.getClass().getSimpleName(); private LocationResult mLocationResult; private double latitude; private double longitude; private double speed; private String time; @Override public void onReceive(Context context, Intent intent) { // Need to check and grab the Intent's extras like so if(LocationResult.hasResult(intent)) { this.mLocationResult = LocationResult.extractResult(intent); //new SaveToFireB().insertToFireBase(mLocationResult.getLastLocation()); new FBSender().put(mLocationResult.getLastLocation()); Log.i(TAG, "Location Received: " + this.mLocationResult.toString()); String msg = String.valueOf(mLocationResult.getLastLocation().getLongitude()) + " " + String.valueOf(mLocationResult.getLastLocation().getLatitude()); // appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ":" + msg, Constants.LOCATION_FILE); } } public void appendLog(String text, String filename) { File logFile = new File(filename); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { //BufferedWriter for performance, true to set append to file flag BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); buf.append(text); buf.newLine(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

这里将调用FBSender将数据发送到firebase。 真正的问题来自这里。 它将仅在前三分钟发送数据,然后它不会将数据发送到firebase为了确认控件是否在那里我将日志消息放在那里,即使在应用程序启动3分钟后,日志消息仍将完美打印

这是FBSender.Java

 public class FBSender extends Service { private String TAG = "FBSender"; private double latitude; private double longitude; private double speed; private String time; @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } public void put (Location location) { latitude = location.getLatitude(); longitude = location.getLongitude(); speed = location.getSpeed(); time = DateFormat.getTimeInstance().format(new Date()); Log.e(TAG, "Entering the run ()"); final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); FirebaseDatabase database = FirebaseDatabase.getInstance(); final DatabaseReference reference = database.getReference("users/" + user.getUid() + "/vehicles"); Log.e(TAG, "IM in the middle"); Map mLocations = new HashMap(); mLocations.put("latitude", latitude); mLocations.put("longitude", longitude); mLocations.put("speed", speed); mLocations.put("time", time); reference.setValue(mLocations); Log.e(TAG, "Exiting The run ()"); } } 

要获得有关数据库写入原因的更多信息,请在3分钟后完成,请将CompetionListener添加到setValue():

 reference.setValue(mLocations, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { if (databaseError == null) { Log.i(TAG, "onComplete: OKAY"); } else { Log.e(TAG, "onComplete: FAILED " + databaseError.getMessage()); } } }); 

当你达到3分钟标记时,如果回调触发错误,例如权限失败,你可以调查原因。 如果它根本停止触发,则可能意味着您已失去与Firebase服务器的连接。 您可以使用侦听器监视连接状态,如文档中所述 。