编写在android中关闭主应用程序时执行的后台程序

时间:2012-03-25 07:27:34

标签: android

我正在编写一个android应用程序,当用户登录到应用程序时,它与服务器通信。现在,如果用户在没有注销的情况下关闭应用程序,我想每15分钟查询一次服务器,以查看特定用户是否收到了任何更新。如果是,那么,我想发一个通知,点击用户直接进入显示更新的应用程序活动。

怎么能在android中实现?有可能吗?
谁能建议使用计时器的解决方案请记住,这个后台程序应该只在实际应用程序关闭时运行。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

是的,这是可能的。

我会做以下事情:

  1. AlarmManagersetRepeating一起使用。这将为您提供15分钟的间隔时间。

  2. setRepeating中,为PendingIntent子类传递IntentService

  3. IntentService子类的handleIntent中,查询您的服务器,然后创建Notification,例如http://developer.android.com/guide/topics/ui/notifiers/notifications.html

  4. Notification将包含另一个PendingIntent,它会将用户带回您的应用。请务必指定包含与该更新相关的UI的Activity

  5. 您可以在http://developer.android.com/guide/topics/fundamentals/services.html

    的服务指南中了解有关IntentServices的更多信息

    您可以在http://developer.android.com/reference/android/app/AlarmManager.html

    了解有关AlarmManager的更多信息