从Android的服务器定期获取数据(轮询) [英] Periodically fetching data (polling) from the server in Android

查看:1167
本文介绍了从Android的服务器定期获取数据(轮询)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序中使用REST调用在那里我从服务器获取数据,并将其添加到视图。我没有得到所有的初始数据。我使用的AsyncTask做了。

I working on the app where I get the data from the server using rest call and add it to the view. I get all the initial data correctly. I use AsyncTask for doing it.

现在我要定期(比如2分钟),从服务器获取新的数据,并把它添加到view.Periodically获取Android中来自服务器的数据(轮询)。

Now I want to periodically (say 2 mins) fetch the new data from the server and add it to view.Periodically fetching data (polling) from the server in Android.

推荐答案

您可以签出AlarmManager类来做到这一点。

You can checkout the AlarmManager class to do it.

Intent intent = new Intent(this, MyAlarmManager.class);

long scTime = 60*2000;//2mins

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + scTime, pendingIntent);

这里的报警管理器 -

here's the alarm Manager--

public class MyAlarmManager extends BroadcastReceiver {

    Context _context;
        @Override
        public void onReceive(Context context, Intent intent) {
            _context= context;
            //connect to server..

        }
}

在有史以来AlarmManager被'开除'连接到服务器再次,并填充你刚才:收到的数据。

when ever the AlarmManager is 'fired' connect to the server again and populate the data you just recieved.

http://developer.android.com/reference/android/app/ AlarmManager.html

这篇关于从Android的服务器定期获取数据(轮询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆