使用AlarmManager同步与服务器数据 [英] Using AlarmManager to sync data with a server

查看:404
本文介绍了使用AlarmManager同步与服务器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为我的英语道歉。

First of all, I apologize for my English.

我需要发送数据到服务器和查询数据在服务器不时,也许从周复一周。

I need to sent data to a server and query for data in that server from time to time, maybe from week to week.

所以我将使用AlarmManager做到这一点。我想知道如果我能在报警管理器注册两地分居未决意图,一是将数据推到服务器,另一个从服务器查询数据。我可以在应用程序每次执行时间注册这两个意图?或者我需要测试,如果我已经有登记的意图?

So I'm going to use the AlarmManager to do that. I wanna know if I can register two separated pending intents in the alarm manager, one to push data to the server, and another one to query data from the server. Can I register those two intents every time the application executes? Or do I need to test if I already got those intents registered?

我问,因为我很害怕,我注册一个警报,从现在开始执行一个星期,然后在用户关闭应用程序,然后再次启动,报警是从现在到底会不会再1周注册推出。

I'm asking that because I'm afraid that I register one alarm to execute one week from now and then the user close the app and start again and the alarm is registered again one week from now and in the end will never be launched.

另一件事,是安全的注册服务与AlarmManager被执行?

Another thing, is it safe to register a service to be executed with the AlarmManager?

呵呵,这是code我使用的是:

Oh and this is the code I'm using:

long WEEK_IN_MILLIS = AlarmManager.INTERVAL_DAY * 7;

PendingIntent querySyncService = PendingIntent.getService(context, 0, new Intent(context,
    QuerySyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);

PendingIntent pushSyncService = PendingIntent.getService(context, 1, new Intent(context,
    PushSyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

alarmManager.setInexactRepeating(AlarmManager.RTC, WEEK_IN_MILLIS, WEEK_IN_MILLIS, querySyncService);

alarmManager.setInexactRepeating(AlarmManager.RTC, WEEK_IN_MILLIS, WEEK_IN_MILLIS, pushSyncService);

我想在把这个code的一些方法内,然后只需调用该方法,每次应用程序启动。

I was thinking in putting this code inside some method and then just call the method every time the app start.

我也看到了,我也许应该使用该解决方案是SyncAdapter,但SyncAdapter似乎只是污垢,有很多code的,什么也不做,例如,以备不时之需存根验证器,再一个存根身份验证服务,一个存根内容提供商,并再次1存根内容提供商服务,那么很多code为同步适配器,然后code的另一个一堆只能运行SyncAdapter。这似乎只是对我来说太肮脏,因为我并不需要一个内容提供商没有一个验证器。

And I also saw that the solution I should probably use is the SyncAdapter, but the SyncAdapter just seems to be dirt, with a lot of code that does nothing, for example, you need one Stub Authenticator, then one Stub Authenticator Service and one Stub Content Provider and again one Stub Content Provider Service, then a lot of code for the Sync Adapter, then another bunch of code to only run the SyncAdapter. This just seems too dirty for me since I don't need a content provider neither one Authenticator.

推荐答案

可以,肯定,注册两个独立的意图,用 AlarmManager 。只要确保在您的呼叫使用不同的ID来 PendingIntent.getService ...(而且,顺便说一句,我建议您不要使用0作为ID。0只是为了来之不易,在Java中)。使用动作,或额外的一个参数,区分它们。

You can, most certainly, register two separate Intents, with the AlarmManager. Just be sure to use different ids in your call to PendingIntent.getService... (and, btw, I suggest that you not use 0 as an id. 0s are just to easy to come by, in Java). Use the ACTION, or a parameter in the Extras, to distinguish between them.

有没有理由使用广播接收器:您可以开火服务直接的意图。如果你是担心安全,不要导出服务。 A 的PendingIntent 从您的应用程序内发射,因此没有必要使服务外部可见的。

There is no reason to use a BroadcastReceiver: you can fire the intent at the Service directly. If you are concerned about security, don't export the Service. A PendingIntent is fired from within your application, so there is no need to make the Service externally visible.

最后,虽然你是对的开销为SyncAdapter,它确实是一个伟大的方式做的正是你正在尝试做的。还有就是如何写一个在 EnterpriseAndroid 一个很好的说明(我是作者之一)。一旦你通过锅炉板得到的,它实际上是几乎优雅。

Finally, although you are right about the overhead for a SyncAdapter, it really is a great way to do exactly what you are trying to do. There is a good description of how to write one in EnterpriseAndroid (I am one of the authors). Once you get through the boiler-plate, it is actually almost elegant.

有一个最小的SyncAdapter 的例子这里

There is an example of a minimal SyncAdapter here.

这篇关于使用AlarmManager同步与服务器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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