机器人 - 如何下载数据的背景在指定时间 [英] Android - How to download data in the background at specified times

查看:102
本文介绍了机器人 - 如何下载数据的背景在指定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉提前没有任何code发布了,主要是因为我不能为我的生活找不出我需要怎么做我需要做的。

I'm sorry in advance for not having any code to post up, mainly because I can't for the life of me figure out how I need to do what I need to do.

基本上,在一天(例如,下午5点)指定的时间间隔,我想我的应用程序,从我的服务器下载一些数据,并将其存储在设备上。这是为了既能减少我的服务器上的负载从具有数据的每个应用程序被运行时被下载,以及减少的加载时间的用户,以便当他们去使用的应用程序,最新的数据已经坐在其设备上

Basically, at specified intervals during the day (ex. 5 P.M), I want my app to download some data from my server and store it on the device. This is to both reduce the load on my server from having data being downloaded every time the app is run, and to reduce the loading times for the user so that when they go to use the app, the latest data is already sitting on their device.

我绝对不知道如何做到这一点。我知道如何下载数据就好了,但现在怎么会在后台下载像我打算。它甚至有可能?

I have absolutely no clue how to do this. I know how to download data just fine, but now how to download in the background like I'm planning. Is it even possible?

我不要求任何人都可以做到这一点对我,但可能有人请点我朝着正确的方向。

I'm not asking for anyone to do it for me, but could someone please point me in the right direction.

推荐答案

使用 AlarmManager

这个类提供访问系统报警服务。这允许您安排您的应用程序,以在未来的某个时候运行。当警报响起时,意图已被注册为它是由系统广播,自动启动目标程序,如果它尚未运行。注册警报被保留,而在设备处于睡眠状态(并且可任选地唤醒设备,如果他们在这段时间去关闭),但如果它被关闭和重新启动将被清除。

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

使用它来启动一个服务

一个服务是一种应用程序组件重新presenting无论是应用程序的愿望,执行长时间运行的操作,而不是与用户交互或提供功能的其他应用程序使用。

A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.

借助 API演示包括报警服务示例(以下简称应用程序一节),其中:

The API Demos includes an Alarm Service example (in the "App" section), which:

演示了如何设定闹钟导致要启动的服务。当你要设定闹钟的启动长时间运行的操作,如检索最新的电子邮件,这非常有用。

Demonstrates how you can schedule an alarm that causes a service to be started. This is useful when you want to schedule alarms that initiate long-running operations, such as retrieving recent e-mails.

在特定的信息,请参阅<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService.html">AlarmService.java使用AlarmManager来安排你的服务的一个例子稍后醒来,看到<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlarmService_Service.html">AlarmService_Service.java对于如何到警报响应的一个例子。该API演示的<一个href="http://developer.android.com/resources/samples/ApiDemos/AndroidManifest.html">AndroidManifest.xml包含相关的服务和活动定义:

In particular, see AlarmService.java for an example of using AlarmManager to schedule your Service to be woken later, and see AlarmService_Service.java for an example of how to respond to that alarm. The API Demo's AndroidManifest.xml contains the related service and activity definitions:

    <service android:name=".app.AlarmService_Service" android:process=":remote" />

    <activity android:name=".app.AlarmService" android:label="@string/activity_alarm_service">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.SAMPLE_CODE" />
        </intent-filter>
    </activity>

这篇关于机器人 - 如何下载数据的背景在指定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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