2014-09-29 79 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