我希望通过给一个时间限制,以更新来自服务器的数据 [英] I want to update the data from server by giving a time limit

查看:179
本文介绍了我希望通过给一个时间限制,以更新来自服务器的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新来自服务器的数据,使用实时调度每15分钟。有没有一种方法,这样我们就可以更新数据后台?

I want to update the data from server for every 15 min using time scheduling. Is there a way, so we can update the data background?

推荐答案

使用报警管理器将其设置为发送广播唤醒你intentservice例如每隔15分钟,并从那里做了更新。

Use the alarm manager set it to send a broadcast to wake up your intentservice instance every 15 mins and do the update from there.

编辑:

我说这样做是为了方便您的引导完成方式,你可能希望在打开你的应用程序,以启动报警的事。无论哪种方式,只要按照codeS,其中报警经理和意图服务的。

I added the boot complete way of doing it for your convenience, you might want to start the alarm thing upon opening your app. Either way just follow the codes where alarm manager and intent service are.

首先创建一个广播接收器监听引导完整

First off create a broadcast receiver that listens for the boot complete

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.CheckUpdateIntentService;

public class BootCompleteReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        //Create pending intent to trigger when alarm goes off
        Intent i = new Intent(context, CheckUpdateIntentService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

        //Set an alarm to trigger the pending intent in intervals of 15 minutes
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        //Trigger the alarm starting 1 second from now
        long triggerAtMillis = Calendar.getInstance().getTimeInMillis() + 1000;
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
    }
}

现在的意图服务做实际的更新

Now for the intent service to do the actual updates

import android.content.Context;
import android.content.Intent;
import android.app.IntentService;

public class CheckUpdateIntentService extends IntentService {

    public CheckUpdateIntentService()
    {
        super(CheckUpdateIntentService.class.getName());
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        //Actual update logic goes here
        //Intent service itself is already a also a Context so you can get the context from this class itself
        Context context = CheckUpdateIntentService.this;
        //After updates are done the intent service will shutdown itself and wait for the next interval to run again
    }
}

在你的Andr​​oidManifest.xml中添加以下内容:

In your AndroidManifest.xml add the following items:

权限收取开机完整的广播

Permission to receive the boot complete broadcast

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

然后在应用程序标记添加你与相应的意图过滤器,你有兴趣创建,并且BootCompleteReceiver当然intentservice成分

Then in the application tags add the BootCompleteReceiver you have created with the corresponding intent filter you are interested in, and of course the intentservice component

<receiver android:name=".BootCompleteReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<service android:name=".CheckUpdateIntentService" ></service>

这是一个非常框架实现,你可以试一下先,如果你需要更多的帮助,让我们知道。

This is a very skeletal implementation, you can try it out first if you need more help, let us know.

这篇关于我希望通过给一个时间限制,以更新来自服务器的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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