Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在后台更新数据_Android_Performance_Service_Android Asynctask - Fatal编程技术网

Android 在后台更新数据

Android 在后台更新数据,android,performance,service,android-asynctask,Android,Performance,Service,Android Asynctask,在我的应用程序中,我需要定期从服务器更新联系人列表、组列表和文件夹列表。我正在将它们保存到“保存首选项”中。目前,我已经实现了一种方法,如果我有我需要的每种类型的列表,我会跳过登录时的更新,并调用后台异步任务,在登录后更新这些数据。问题是,连接很低,用户可以登录,但是他们什么也不能做,等待后台更新阻止其他http请求。 如何定期刷新此数据?例如,即使应用程序未激活,也可以更新数据的服务。您可能应该使用服务 manifest.xml 开始服务 它可以在设备后台定期运行,而无需应用程序的请求?是的,

在我的应用程序中,我需要定期从服务器更新联系人列表、组列表和文件夹列表。我正在将它们保存到“保存首选项”中。目前,我已经实现了一种方法,如果我有我需要的每种类型的列表,我会跳过登录时的更新,并调用后台异步任务,在登录后更新这些数据。问题是,连接很低,用户可以登录,但是他们什么也不能做,等待后台更新阻止其他http请求。 如何定期刷新此数据?例如,即使应用程序未激活,也可以更新数据的服务。

您可能应该使用服务

manifest.xml

开始服务


它可以在设备后台定期运行,而无需应用程序的请求?是的,服务在后台运行
<service
    android:name="MyService"
    android:icon="@drawable/icon"
    android:label="@string/service_name">
</service> 
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);