我正在编写一个android应用程序,当用户登录到应用程序时,它与服务器通信。现在,如果用户在没有注销的情况下关闭应用程序,我想每15分钟查询一次服务器,以查看特定用户是否收到了任何更新。如果是,那么,我想发一个通知,点击用户直接进入显示更新的应用程序活动。
怎么能在android中实现?有可能吗?
谁能建议使用计时器的解决方案请记住,这个后台程序应该只在实际应用程序关闭时运行。
答案 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)
是的,这是可能的。
我会做以下事情:
将AlarmManager
与setRepeating
一起使用。这将为您提供15分钟的间隔时间。
在setRepeating
中,为PendingIntent
子类传递IntentService
在IntentService
子类的handleIntent
中,查询您的服务器,然后创建Notification
,例如http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Notification
将包含另一个PendingIntent
,它会将用户带回您的应用。请务必指定包含与该更新相关的UI的Activity
。
您可以在http://developer.android.com/guide/topics/fundamentals/services.html
的服务指南中了解有关IntentServices的更多信息您可以在http://developer.android.com/reference/android/app/AlarmManager.html
了解有关AlarmManager的更多信息