2014-09-29 60 views
0

在我的應用程序中,我需要定期更新從服務器更新的聯繫人列表,組列表和文件夾列表。我現在將它們保存到保存偏好設置中。目前我已經實現了一種方法,如果我有我需要的每種類型的列表,我會跳過登錄更新並調用一個後臺asyncTask,它在登錄後更新此數據。問題在於用戶可以登錄的連接很低,但是他們不能做任何事情,等待阻止其他http請求的後臺更新。 如何定期刷新這些數據?就像即使應用程序未處於活動狀態也會更新數據的服務一樣。在後臺更新數據

+0

發佈您的代碼。 – 2014-09-29 07:28:37

回答

0

您應該使用Service

Android Service Tutorial

的manifest.xml

<service 
    android:name="MyService" 
    android:icon="@drawable/icon" 
    android:label="@string/service_name"> 
</service> 

MyService.java

public class MyService extends Service { 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    //TODO do something useful 
    return Service.START_NOT_STICKY; 
} 

@Override 
public IBinder onBind(Intent intent) { 
    //TODO for communication return IBinder implementation 
    return null; 
} 
} 

啓動服務

// use this to start and trigger a service 
Intent i= new Intent(context, MyService.class); 
// potentially add data to the intent 
i.putExtra("KEY1", "Value to be used by the service"); 
context.startService(i); 
+0

它可以在沒有來自應用程序的請求的情況下在設備背景中定期運行? – fustalol 2014-09-29 07:39:04

+0

是的,服務在後臺運行 – R9J 2014-09-29 07:40:47