服务中的Firebase将在应用程序停留一段时间后停止工作

时间:2015-05-22 12:56:07

标签: android service firebase firebase-realtime-database

这就是我的应用程序有一个服务(扩展服务)负责建立套接字连接和使用firebase本机android框架监听进入网络流量。这就是它的样子

public class SocketService extends Service {

private Firebase firebase;

@Override
public void onCreate() {
    super.onCreate();

    firebase = AppHelper.getInstance().getFirebase().child(AppHelper.getInstance().getFirebase().getAuth().getUid());
    AppHelper.getInstance().categoriesController.setListeners(firebase.child("categories"));
    AppHelper.getInstance().tablesController.setListeners(firebase.child("tables"));
    AppHelper.getInstance().itemsController.setListeners(firebase.child("items"));
    AppHelper.getInstance().requestsController.setListeners(firebase.child("requests"));
    AppHelper.getInstance().ordersController.setListeners(firebase.child("orders"));
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
    AppHelper.getInstance().categoriesController.removeListeners(firebase.child("categories"));
    AppHelper.getInstance().tablesController.removeListeners(firebase.child("tables"));
    AppHelper.getInstance().itemsController.removeListeners(firebase.child("items"));
    AppHelper.getInstance().ordersController.removeListeners(firebase.child("orders"));
    AppHelper.getInstance().requestsController.removeListeners(firebase.child("requests"));
    AppHelper.getInstance().clearUserData();
    AppHelper.getInstance().waiter = null;
    AppHelper.getInstance().setConnectionState(false);
}
}

正如你所看到onStartCommand返回START_NOT_STICKY,所以一旦我离开应用程序(在背景 - 中间按钮),服务就不会被杀死

以下是AppHelper的工作原理:

public class AppHelper extends Application {

private static AppHelper instance = null;
private static Bus bus;
public TablesController tablesController;
public OrdersController ordersController;
public ItemsController itemsController;
public RequestsController requestsController;
public CategoriesController categoriesController;
private Firebase firebase;
public Waiter waiter;
private boolean connected = false;
public static AppHelper getInstance() {
    return instance;
}
public Bus getBus() {return bus;}

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
    bus = new Bus();
    tablesController = new TablesController(this);
    ordersController = new OrdersController(this);
    itemsController = new ItemsController(this);
    requestsController = new RequestsController(this);
    categoriesController = new CategoriesController(this);

    Firebase.setAndroidContext(this);
    firebase = new Firebase("https://restapp.firebaseio.com/");
    // other setup code
}

public Firebase getFirebase() {
    return firebase;
}

public void clearUserData() {
    tablesController.clearData();
    ordersController.clearData();
    itemsController.clearData();
    requestsController.clearData();
    categoriesController.clearData();
}

public void setConnectionState(boolean connected) {
    this.connected = connected;
    bus.post(new NetworkStateEvent(connected));
}

public boolean isConnected() {
    return connected;
}
}

每个控制器都将firebase侦听器注册到相应的节点,以下是基类的外观

public abstract class BaseController<T> {
protected Handler handler;
protected Firebase node;
protected ArrayList<T> models = new ArrayList<>();
protected ChildEventListener eventListener;

public BaseController(Context context) {
    handler = new Handler(context.getMainLooper());
    eventListener = getEventListener();
}

public void setListeners(Firebase node) {
    this.node = node;
    node.addChildEventListener(eventListener);
}

abstract ChildEventListener getEventListener();

public void removeListeners(Firebase node) {
    models.clear();
    node.removeEventListener(eventListener);
}

public ArrayList<T> getItems() {
    return models;
}

public void clearData() {
    models.clear();
}
}

所以,这里发生了什么。一切都很好,如果你把应用程序留在后台并返回它仍然有效,你甚至可以启动其他应用程序,锁定设备等。 但是,如果您将应用程序留在背景上足够长(10分钟左右),当您回来时,firebase将不会响应。该应用程序不会崩溃,但它不会做任何事情,因为它是离线的。我猜是收集到的东西正在收集。但我不知道那可能是什么。

我试过了这个

connectedRef = new Firebase("https://restapp.firebaseio.com/.info/connected");

    connectionStateListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            boolean connected = snapshot.getValue(Boolean.class);
            AppHelper.getInstance().setConnectionState(connected);
        }

        @Override
        public void onCancelled(FirebaseError error) {
            System.err.println("Listener was cancelled");
        }
    };
    connectedRef.addValueEventListener(connectionStateListener);

每当firebase断开连接时,都会在用户的片段上显示一条消息。当我在10分钟后回到应用程序时,此消息不存在(该firebase已连接?)。

提前谢谢你!

0 个答案:

没有答案