java 定期从 Android 中的服务器获取数据(轮询)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3607351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:37:54  来源:igfitidea点击:

Periodically fetching data (polling) from the server in Android

javaandroidandroid-asynctask

提问by Kunal P.Bharati

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.

我在使用 rest 调用从服务器获取数据并将其添加到视图的应用程序上工作。我正确地得到了所有的初始数据。我使用 AsyncTask 来做这件事。

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.

现在我想定期(比如 2 分钟)从服务器获取新数据并将其添加到 view.Periodically 从 Android 中的服务器获取数据(轮询)。

回答by Umesh

You can checkout the AlarmManager class to do it.

您可以查看 AlarmManager 类来执行此操作。

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..

        }
}

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

当 AlarmManager 被“触发”时,再次连接到服务器并填充您刚刚收到的数据。

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

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

回答by sairajat

follow the tutorial mentioned here. This is exactly what you want to do. Also since server calls are made every few mins this consumes battery. So you can try for server calls only when the server data changes through Push notifications ..

按照此处提到的教程进行操作。这正是您想要做的。此外,由于服务器调用每隔几分钟进行一次,这会消耗电池。所以你只能在服务器数据通过推送通知发生变化时尝试服务器调用..

回答by Miguel Morales

The best way to do it would be to create a service that fetches the data from the server. Afterward if your activity is running, the service can send an intent to the activity with the fetched data.

最好的方法是创建一个从服务器获取数据的服务。之后,如果您的 Activity 正在运行,则服务可以使用获取的数据向 Activity 发送意图。

Or, have the service run when your app runs and have your activity bind to the service when it start up. Then use AIDL or something similar to communicate with the service.
(For example, every time the service has fetched data, it can fire off a callback function in your activity)

或者,在您的应用程序运行时运行该服务,并在它启动时将您的活动绑定到该服务。然后使用 AIDL 或类似的东西与服务进行通信。
(例如,每次服务获取数据时,它都可以在您的活动中触发回调函数)