java - Android - 如何在指定时间在后台下载数据

标签 java android background sync

我很抱歉没有任何代码可以发布,主要是因为我无法为我的生活弄清楚我需要如何做我需要做的事情。

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

我完全不知道该怎么做。我知道如何很好地下载数据,但现在如何像我计划的那样在后台下载。有可能吗?

我不要求任何人为我做这件事,但有人能给我指出正确的方向吗?

最佳答案

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

用它来启动 Service

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 Demos包括一个警报服务示例(在“应用程序”部分),其中:

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.

具体参见 AlarmService.java有关使用 AlarmManager 安排稍后唤醒您的服务的示例,请参阅 AlarmService_Service.java有关如何响应该警报的示例。 API 演示的 AndroidManifest.xml包含相关的服务和 Activity 定义:

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

关于java - Android - 如何在指定时间在后台下载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3233764/

相关文章:

html - 背景与 div 垂直对齐

android - 添加 9-patch 作为按钮背景后无法对齐文本

java - 如何在 Bukkit 插件中创建自定义配置文件?

java - 我的程序一直说该方法无法解析

java - 将字节值转换为 int

Android 印度尼西亚语文本转语音

Java 无法用于 Opencv 安装

android - 清除 fragment 堆栈以转到另一个 fragment

java - 如何设置编辑文本框的布局边距?

ios - 在 iOS 上停止另一个应用程序的背景音频