FirebaseMessagingService是一项意向服务吗?

时间:2017-02-27 09:14:57

标签: android firebase firebase-cloud-messaging

我想在SQLite数据库中保存服务的onMessageReceived中收到的所有消息。

我打算打开数据库,插入数据并关闭数据库。这对于意图服务很有效,因为对onMessageReceived的所有调用都将排队,从而逐个执行。

但是,如果同时为多个消息调用onMessageReceived,则可能导致Db被关闭而另一个调用正在尝试写入因此导致问题。

任何人都可以确认我应该期待什么样的行为。

如果它不是意图服务,我可能需要查看数据库单例模式和同步块

2 个答案:

答案 0 :(得分:1)

当前,FirebaseMessagingService直接扩展Service,因此它不是IntentService

Source
快照:

  

enter image description here

答案 1 :(得分:0)

您可以检查LinkedBlockingDeque<>类,它将您的请求排队,并且可以按顺序在后台执行。
检查我的以下样本类 -

public class Processor implements Runnable {

private final LinkedBlockingDeque<Task> mTaskQueue = new LinkedBlockingDeque<Task>();
private boolean mExecuteTask = true;

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    // You can put some end  condition if needed
    while (mExecuteTask) {
        Task task;
        try {
            // Waits if necessary until an element becomes available
            task = (Task) mTaskQueue.take();
        } catch (InterruptedException e) {
            continue; // re-test the condition on the eclosing while
        }
        if (task != null) {
            task.runnable.run();
        }
    }
}

// Call this method to put Runnable in Queue.
public synchronized void put(int id, Runnable runnable) {
    try {
        Task task = new Task();
        task.runnable = runnable;
        task.id = id; // someUniqueId to avoid duplication
        // Check Id of tasks already present in Queue
        mTaskQueue.addLast(task);
    } catch (IllegalStateException ie) {
        throw new Error(ie);
    }
}

private static class Task {
    public Runnable runnable;
    // Unique Id of task
    public int id;
}

}