从服务更新活动数据时,其暂停 [英] Update Activity data from service when its paused

查看:215
本文介绍了从服务更新活动数据时,其暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有,有一个数据对象的活动。它更新基于该对象在其GUI组件。现在让我们说,它已暂停(在onPause叫),但不停止或destoryed

Lets say I have an activity that has a data object. It updates its gui component based on that object. Now lets say that it is paused ( OnPause is called) but not stopped or destoryed

同时,推送通知接收(intentservice开始),我需要更新的推送通知的对象,所以我的GUI时,应用程序恢复更新该对象。

Meanwhile , a push notification is received ( intentservice is started) and I need to update that object with the push notification object so I gui is updated when the app is resumed.

我想过发送广播的活动,因此它可以更新其数据对象,但我读的地方,当活动被暂停然后广播都没有收到。

I thought about sending a broadcast to the activity so it can update its dataobject But I read somewhere that when activity is paused then broadcasts are not received.

我应该在这种情况下怎么办?

What should I do in this case?

推荐答案

的辛苦,但正确的方式做,这是建立自己的自定义的ContentProvider 为您的应用程序和更新从Web服务收到推送通知这个的ContentProvider 的所有数据。当活动回来推向前台,它更新本身所提供的新数据的ContentProvider

The hard but correct way to do this is to build your own custom ContentProvider for your app and update all data received from web services and push notifications to this ContentProvider. When the Activity comes back into the foreground, it updates itself with the new data provided by the ContentProvider.

硬盘的,因为做一个自定义的的ContentProvider 是一个大量的工作。它是的正确的方式,因为它是为了符合移动应用的行为,并与Android架构:说一个用户激活一个Web服务或一些计算密集型任务,然后驳回应用;或者说,推送通知到达,需要应用程序的数据进行更新和显示。在这两种情况下,应用程序的活动 S可不再在前台,但服务可用于执行一些非UI操作。现在,在该操作结束时,服务使得通过的ContentProvider ,和用户激活时更改数据再次应用程序中,活动■从得到他们的新数据的ContentProvider

It is hard because making a custom ContentProvider is a lot of work. It is the correct way because it is in conformance with the behavior of mobile applications and with the Android architecture: say a user activates a web-service or some computation-intensive task, and then dismisses the app; or say a push notification arrives and requires the app's data to be updated and displayed. In both cases, the app's Activitys may no longer be in the foreground, but a Service can be used to perform some non-UI operation. Now at the end of that operation, the Service makes changes to the data through the ContentProvider, and when the user activates the app again, the Activitys get their new data from the ContentProvider.

要引用href=\"http://developer.android.com/guide/topics/providers/content-providers.html\" rel=\"nofollow\">官方教程的

To quote the official tutorial:

内容提供商,在一个连接数据的标准接口
  与code在另一个进程中运行的进程。

Content providers are the standard interface that connects data in one process with code running in another process.

作为一名开发人员,你应该总是假定用户可以随时调用应用程序,并在任何时候驳回。无论是否是活动的在前台与否,需要正确地更新和维护应用程序的数据。

As a developer, you should always assume that the user may invoke an app at any time and dismiss it at any time. Irrespective of whether an Activity is in the foreground or not, the app's data needs to be correctly updated and maintained.

谷歌自己的应用程序中使用自定义的ContentProvider 秒。特别是的Gmail 应用程序利用了它的的ContentProvider 来获得新的电子邮件时网络连接不可用,并显示电子邮件脱机。在的Facebook 的WhatsApp &安培; 微博 Android应用也使得使用的ContentProvider 秒。

Google's own apps use custom ContentProviders. The Gmail app in particular makes use of its ContentProvider to get new emails when network connectivity is available and display emails offline. The Facebook, WhatsApp & Twitter Android apps also make use of ContentProviders.

这篇关于从服务更新活动数据时,其暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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