【问题标题】:Android: Firebase Update Issue When the app is in backgroundAndroid:应用程序处于后台时的 Firebase 更新问题
【发布时间】:2016-07-06 15:09:04
【问题描述】:

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

提前致谢

BackgroundLocation.java 它在后台运行以获取位置详细信息并调用广播 Reveive。 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();
    }
}
}

Here 将调用 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, "I M 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 ()");

}
}

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    要获取有关数据库写入为何在 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 服务器失去了连接。您可以使用监听器监控连接状态,如described in the documentation

    【讨论】:

    • 嘿,谢谢,我什至按照你的建议做了,但是直到 3 分钟它才会打印 07-06 23:08:41.972 10540-10540/com.geekyint.instapo I/FBSender: onComplete:好的 3 分钟后它不会打印任何内容,即 databaseError 消息,我认为它不会用于其他部分
    • 听起来您在 3 分钟后失去连接。
    • 如何刷新连接?
    • 你不应该这样做。如果需要,它应该保持连接。尝试使用FirebaseDatabase.setLogLevel() 启用调试日志。调试日志消息包含大量有关连接状态的信息。
    • 虽然您的症状与this other issue 不同,但您可能想看看它的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多