0

我的應用程序應該檢查每X分鐘。網頁內容和我必須實現wakeLock,因爲它應該在屏幕關閉時工作,但同時我必須使用AsyncTask,因爲它使用網絡(在主線程中禁用它)。背景服務wakelock和線程

確保.release()會被調用的好方法是什麼?

Block PostExecute並非全部調用(當doInBackground()中有錯誤時),最後在主線程中也是如此。

回答

0

如果您使用Google Cloud Messaging,則可以在設備請求服務器數據獲取時使用定義的X分鐘時間從服務器進行控制。您的移動應用程序將對更新的服務器數據做出最大響應,並且我們將減少服務器負載以僅在需要時加載。

GCM:入門 http://developer.android.com/google/gcm/gs.html

當你實現GCM,您的服務器可以發送發送到同步或郵件與有效載荷的設備。 (http://developer.android.com/google/gcm/adv.html

在您的移動應用程序中,您將獲得一個類,extends GCMBaseIntentService,您將收到一個onMessage調用,在這裏您可以發出服務器請求併發出通知。

@Override 
protected void onMessage(Context context, Intent intent) { 
    try { 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoOutput(true); 
     conn.setUseCaches(false); 
     conn.setFixedLengthStreamingMode(bytes.length); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded;charset=UTF-8"); 
     // post the request 
     OutputStream out = conn.getOutputStream(); 
     out.write(bytes); 
     out.close(); 
     // handle the response 
     int status = conn.getResponseCode();    
... 
+0

不,我不使用GCM - 我終於找到了一些相關的問題(和答案 - 我在下面發佈)。 – 2013-05-07 18:54:43