创建一个Android后台服务,该服务连续轮询REST API以获取数据 [英] Creating an Android background service that continuously polls a REST API for data

查看:159
本文介绍了创建一个Android后台服务,该服务连续轮询REST API以获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个Android服务,该服务会轮询服务器以获取数据,解析数据,然后通过意图将其发送到应用程序.轮询必须频繁进行(每隔几秒钟).根据我的阅读,不建议像这样进行轮询(由于电池寿命问题).这是我第一次使用Android进行开发,经过大量研究后,还有一些我不清楚的事情.

I need to write an Android service that polls a server for data, parses the data, and then sends it to an app via intents. The polling must occur frequently (every few seconds). From what I've read, polling like this is not recommended (due to battery life concerns). This is my first time developing in Android, and after doing a lot of research there are a few things that remain unclear to me.

我不确定服务,同步适配器或警报管理器是否更适合我的需求.在这个问题的背景下,哪一个最有意义?

I am unsure if a service, sync adapter, or alarm manager would be better suited for my needs. Which of these makes most sense in the context of this problem?

此服务需要在启动时启动,并继续在后台运行.轮询服务器后,数据将通过意图发送到另一个应用程序.该服务应该完全没有用户交互.从我所读的内容来看,Android似乎试图阻止人们由于恶意软件问题而写这类东西.有可能做到这一点吗?

This service needs to start on boot and continue to run in the background. After polling the server, the data is sent to another app via intents. The service should have no user interaction at all. From what I've read it seems like Android tries to prevent people from writing this sort of thing due to malware concerns. Is it possible to accomplish this?

推荐答案

首先,创建一个服务类,该服务类将在给定的时间间隔后运行处理程序,

First of all, create a service class that will run a handler after a given time interval,

  public class SyncService extends Service {

   private Handler mHandler;
   // default interval for syncing data
    public static final long DEFAULT_SYNC_INTERVAL = 30 * 1000;

        // task to be run here
        private Runnable runnableService = new Runnable() {
            @Override
            public void run() {
                syncData();
                // Repeat this runnable code block again every ... min
                mHandler.postDelayed(runnableService, Constant.DEFAULT_SYNC_INTERVAL);
            }
        };

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // Create the Handler object
            mHandler = new Handler();
            // Execute a runnable task as soon as possible
            mHandler.post(runnableService);

            return START_STICKY;
        }

        private synchronized void syncData() {
            // call your rest service here
        }
    }

syncData()方法内,放置您的REST API调用,该调用将在30秒的间隔后被调用.

Inside the syncData() method put your REST API call which will be called after a 30 sec interval.

关于在启动时启动服务,请使用广播接收器,该启动器将在启动完成后触发服务,例如,

Regarding the service startup on boot, use a broadcast receiver that will trigger the service once the boot is complete, for example,

<receiver android:name=".BootCompletedIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

剩下的事情取决于您!请先尝试一下,然后再寻求帮助!

The rest of the thing is up to you! Please try yourself first and ask back for help!

这篇关于创建一个Android后台服务,该服务连续轮询REST API以获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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