有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我事先很抱歉没有任何代码可以发布,主要是因为我一辈子都不知道该怎么做

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

我完全不知道怎么做。我很清楚如何下载数据,但现在我正在计划如何在后台下载。有可能吗

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


共 (3) 个答案

  1. # 1 楼答案

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

    {a3}包括一个报警服务示例(在“应用”部分中),该示例:

    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包含相关的服务和活动定义:

        <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>
    
  2. # 2 楼答案

    could someone please point me in the right direction.

    AlarmManagerServiceAsyncTaskBroadcastReceiver

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