2012-07-13 55 views
0

我遇到了一些問題。在Android上保存未執行的網絡操作

我有一個運行Android的應用程序,它很棒。 我使用「cleanservice」將操作存儲在BlockingQueue中,然後在服務運行的每一分鐘執行一次。 現在,當用戶想要關閉應用程序時,就會出現問題。當他想關閉時,我最後一次運行該服務,通過互聯網發送所有操作。

但是,當沒有互聯網連接可用時,我所有排隊的操作都會丟失。

我需要一種方法來存儲這些操作(如在sharedpreferences或文件或...)時,如果他們不能發送關閉。當我再次打開應用程序時,如果有未執行的操作需要發送,我可以嘗試。我發現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(); 

}

回答

0

您可以設置一個SQLite數據庫,並跟蹤你的行動通過的。這會讓您的隊列過時,並且您的操作始終已經存儲,以防應用程序強行關閉。

或簡單地將它們序列化(JSON?)到共享首選項。

+0

我與Json合作是的,所以我會給sharedPreferences一個嘗試。 – Robin 2012-07-16 14:30:05