在背景间隔呼叫网络服务

时间:2014-04-10 23:53:27

标签: android

应用程序需要在间隔(例如每30分钟)中同步服务器中的数据或在后台手动同步数据。结果将保存到本地数据库。同步完成后,我想提醒活动/片段...并强制更新列表(如果需要)。有很多活动,所以我想把它移到活动之外,使它更加一致。

现在我创建了AsyncTask,它从服务器获取结果并保存到DB。

我用什么? BroadcastReciever,Service,AlarmManager?


更新

根据答案,我在应用

中启动闹钟
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

中注册了Receiver and Service
<service
    android:name="com.cloudit.tsystems.app.DataSyncer"
    android:enabled="true">
</service>
<receiver
    android:name="com.cloudit.tsystems.app.AlarmReceiver"
    android:enabled="true">
</receiver>

我在何处以及如何通知活动/片段中的同步?

2 个答案:

答案 0 :(得分:3)

我会使用AlarmManager并注册BroadcastReceiver。一旦接收器被触发,我将启动IntentService以在后台下载数据。

配置闹钟:

    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,当闹钟响起时会收到通知。请注意,我正在使用WakefulBroadcastReceiver,以便在您同步时设备不会进入休眠状态。

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:

class DataSyncer extends IntentService{

    ...

    @Override
    protected void onHandleIntent(Intent intent) {
       //sync data
       MyBroadcast.completeWakefulIntent(intent);
    }

}

<强>更新 现在您已经同步了数据,有几个选项可以通知活动和碎片。您可以使用LocalBroadcastManager进行广播。有关详细信息,请查看this链接。

答案 1 :(得分:1)

使用AlarmManager以30分钟的间隔触发PendingIntent,启动IntentService进行下载。

Intent intent = new Intent(context, PollingService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(
                AlarmManager.RTC,
                System.currentTimeMillis(),
                AlarmManager.INTERVAL_HALF_HOUR,
                pendingIntent
);

当IntentService完成更新数据后,它可以发送您的活动/片段已注册的广播,以通知其新数据并刷新其数据。

sendBroadcast(new Intent("DATA_UPDATED"));

在你的片段中

getActivity().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //update UI
        }
    }, new IntentFilter("DATA_UPDATED"));