在后台区间调用Web服务 [英] Call web service in interval on background
问题描述
应用程序需要的时间间隔(每次30min例如)手动背景从服务器同步数据或。结果保存到本地数据库。同步完成后我想提醒活动/片段......和力的更新列表(如果需要)。有很多的活动,所以我想将它的活动之外,使其更加一致。
Application need synchronize data from server in interval (for example every 30min) or manually on background. Result is saved to local database. After sync is done I want remind activity/fragment ... and force update list (if needed). There are many activities, so I want move it outside of activity and make it more consistent.
现在我创建的AsyncTask其从服务器产生并保存到数据库中。
For now I created AsyncTask which get result from server and save to DB.
我768,16使用? BroadcastReciever,服务,AlarmManager?
What shoud I use? BroadcastReciever, Service, AlarmManager?
更新
根据答案我开始的报警在应用程序
Based on answers I start alarm in Application
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setInexactRepeating(AlarmManager.RTC, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
创建的接收
public class AlarmReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent syncIntent = new Intent();
syncIntent.setClass(context, DataSyncer.class);
startWakefulService(context, syncIntent);
}
}
创建 IntentService
public class DataSyncer extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
// get data from server
// save to DB
AlarmReceiver.completeWakefulIntent(intent);
}
}
和注册接收器,服务于 AndroidManifest
And registered Receiver and Service in AndroidManifest
<service
android:name="com.cloudit.tsystems.app.DataSyncer"
android:enabled="true">
</service>
<receiver
android:name="com.cloudit.tsystems.app.AlarmReceiver"
android:enabled="true">
</receiver>
在哪里,我怎么通知同步在活动完成/片段?
Where and how I notify that sync is done in Activity/Fragment?
推荐答案
我会用AlarmManager并注册一个BroadcastReceiver。一旦接收器被触发,我将推出IntentService到在后台下载的数据。
I would use the AlarmManager and register a BroadcastReceiver. Once the receiver is fired, I will launch an IntentService to download the data in the background.
配置您的报警:
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyBroadcast.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.setInexactRepeating(AlarmManager.RTC, 0, AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
创建将得到通知一个BroadcastReceiver当警报响起。请注意,我使用的是<一个href=\"https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html\"相对=nofollow> WakefulBroadcastReceiver 从而使设备不下去的时候,你要同步进入梦乡。
Create a BroadcastReceiver that will get notified when the alarm goes off. Note that I'm using a WakefulBroadcastReceiver so that the device doesn't go to sleep when you're syncing.
class MyBroadcast extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent syncIntent = new Intent();
syncIntent.setClass(context, DataSyncer.class);
startWakefulService(context, syncIntent);
}
}
接着,IntentService将在后台中下载数据
Next, an IntentService that will download data in the background:
class DataSyncer extends IntentService{
...
@Override
protected void onHandleIntent(Intent intent) {
//sync data
MyBroadcast.completeWakefulIntent(intent);
}
}
更新:
所以,现在你有你的数据同步,有几个选项来通知活动和片段。您可以使用LocalBroadcastManager播出。看看此联系了解更多详情。
这篇关于在后台区间调用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!