2012-03-25 62 views
1

我正在编写一个android应用程序,当用户登录到应用程序时与服务器进行通信。现在,如果用户关闭应用程序而不注销,我希望每15分钟查询一次服务器,以查看特定用户是否收到任何更新。如果是的话,我想推送一个通知,点击用户直接进入显示更新的应用程序活动。

这怎么可以在android中实现?可能吗?
任何人都可以提出解决方案使用计时器?请记住,只有在实际应用程序关闭时,此后台程序才能运行。编写一个后台程序,当主应用程序关闭时执行android

回答

1

是的,这是可能的。

我会做到以下几点:

  1. 使用AlarmManagersetRepeating。这会让你以15分钟的时间间隔。

  2. setRepeating,传递一个PendingIntentIntentService

  3. 在你IntentService子类,在handleIntent,查询您的服务器,然后创建一个Notification像记录在http://developer.android.com/guide/topics/ui/notifiers/notifications.html

  4. Notification将包含另一个PendingIntent这会将用户带回您的应用。确保指定包含与该更新相关的用户界面的Activity

您可以了解更多有关服务指南中IntentServices在http://developer.android.com/guide/topics/fundamentals/services.html

您可以了解更多关于AlarmManager在http://developer.android.com/reference/android/app/AlarmManager.html

+0

即使应用程序本身已关闭,意向服务是否仍会运行? – Ashwin 2012-03-25 10:55:49

+0

即使没有可见的活动,AlarmManager也会启动您的IntentService。这已在http://developer.android.com/reference/android/app/AlarmManager.html>中有详细说明。注意:Alarm Manager用于希望让应用程序代码在特定时间运行的情况,即使你的应用程序目前没有运行。 – louielouie 2012-03-25 22:39:43

+0

谢谢你的答案。你能否也请指出一些教程或一些例子来描述使用报警管理器来调用一个意图服务? – Ashwin 2012-04-01 06:20:08

2

您可以使用服务来达到此目的。采取看看这个: http://developer.android.com/guide/topics/fundamentals/services.html http://marakana.com/forums/android/examples/60.html http://developer.android.com/guide/topics/ui/notifiers/notifications.html

+0

即使在应用程序本身关闭将服务运行? – Ashwin 2012-03-25 13:01:32

+0

是的。服务通常用于在后台进行这种长时间运行的操作。如果你停止你的应用程序,你的服务将继续工作。服务可以自行停止,也可以由系统或启动该服务的应用程序停止。 – Emran 2012-03-25 14:09:41

相关问题