在android上保存未执行的网络操作

时间:2012-07-13 14:00:35

标签: android networking service storage

我遇到了一些问题。

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

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

如果无法发送,我需要一种方法来存储这些操作(如在sharedpreferences或file或...中)。当我再次打开应用程序时,如果有未执行的操作需要发送,我可以解决。

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

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();

}

1 个答案:

答案 0 :(得分:0)

您可以设置SQLite数据库并按此跟踪您的操作。这将使您的队列过时,并且在应用程序被强制关闭的情况下,您的操作始终已存储。

或者只是将它们序列化(JSON?)存储到共享偏好中。