0

我有点问题。

我有一个正在运行的 android 应用程序,非常棒。我使用“cleanservice”将操作存储在 BlockingQueue 中,然后每分钟执行一次服务运行。现在,当用户想要关闭应用程序时,就会出现问题。当他想关闭时,我最后一次运行该服务以通过 Internet 发送所有操作。

但是当没有可用的互联网连接时,我所有排队的操作都会丢失。

如果无法发送,我需要一种方法来在关闭时存储这些操作(例如在 sharedpreferences 或文件中或......)。当我再次打开应用程序时,我可以检查是否有未执行的操作需要发送。

我发现几种存储数据的方法。但是最好的方法是什么?

if(!finaltry)
                processActionQueues(true, true);
            else{
                // We get here when we are logging off (shutting down) and our final try has failed to send the updates to the server
                // We are going to save the actions so we can send them when we log on again

            }

我的操作来自这个界面:

public interface IWebServiceAction {
/**
 * Executes the action on the server.
 * Here you would typically do an http request to the webservice 
 * @param ctx
 * @return
 * @throws IOException
 * @throws AuthenticationException 
 */
boolean execute(Context ctx) throws IOException, AuthenticationException;
/**
 * Indicates whether this is an urgent action. If the action is
 * to be executed by the CleanService then it will execute it
 * immediately if the action is urgent.
 * Default = false
 * @return
 */
boolean isUrgent();
/**
 * If the user needs to be logged into the system before we can
 * execute the action then isPrivate is true.
 * Public actions that can be executed regardless of login are for
 * example the login action itself, or a check for updates.
 * Default = true
 * @return
 */
boolean isPrivate();

/**
 * Indicates whether this action requires that all previous actions are 
 * executed before we begin executing this one
 * @return
 */
boolean requiresQueueuToBeCompleted();

}

4

1 回答 1

0

You can set up an SQLite database and keep track of your actions by that. That would make your queue obsolete and your action are always already stored in case the app gets force closed.

Or simply store them serialized (JSON?) to sharedpreferences.

于 2012-07-13T14:29:00.627 回答