获取的onPause和放大器; onResume喜欢的应用程序/任务级事件 [英] Get onPause & onResume like events at application/task level

查看:263
本文介绍了获取的onPause和放大器; onResume喜欢的应用程序/任务级事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么可能是没有回调在应用层面,当应用程序被切换到后台,并来到前台的原因。 活动类的<​​code>的onPause 和 onResume 只要求当前顶。如果我想停止某些后台任务具有应用程序级别范围,那么有没有简单的方法,当应用程序被切换到后台我能阻止它。有这些事件回调高的要求。

I was wondering what could be the reason for not having a callback at the application level when an application goes to background and comes to the foreground. Activity class's onPause and onResume are only called on the current top activity. If I want to stop some background task that has the application level scope, then there is no easy way I can stop it when the app goes to background. There is high demand for these event callbacks.

为什么不机器人对暂停和应用的简历应用程序级别的回调?可以说,它是Android中的任务(活动堆栈)级实施,如果不是在应用层面?

Why doesn't Android have a app level callback on pause and resume of applications? Can it be implemented in Android at the task(activity stack) level if not at app level?

有一个背景的TimerTask刷新UI与Web数据定期。当应用程序不再在前台我想阻止它。

A background Timertask refreshes UI with data from web at regular intervals. When app is no longer in foreground I want to stop it.

目前,我把重复code在 BaseActivity 。这是在活动水平。该任务将停止,并开始对每一个活动的每次暂停和恢复以及应用程序的情况下,将BG或即将FG是隐藏在这些活动中,我不知道一个。我想知道是否有这样做的更好的办法,我的意思是,知道什么时候该应用程序已不再是用户可见的。

Currently I am putting the repeating code in a BaseActivity. This is at the activity level. The task stops and starts on each pause and resume of every activity and the event of app going to bg or coming to fg is hidden among one of these events, which I cannot know. I wanted to know if there is a better way of doing it, I mean knowing when the app has stopped being visible to the user.

推荐答案

使用处理器,而不是TimerTask的。如果活动不活跃绑定到一个活动的尺蠖一个处理程序定时信息不会被触发。

Use Handler instead of TimerTask. A timed message for a Handler bound to an Activity's Looper will not be triggered if the activity is not active.

的图案将是发送定时信息到处理程序这又将产生一个线程/ AsyncTask的这将在后台执行请求和更新UI。在线程创建/销毁更多的开销,但是,这是I / O反正约束。你可以让一个线程池,如果该开销成为bototleneck(虽然我怀疑它会)。

The pattern would be to send a timed message to the Handler which would in turn spawn a Thread/AsyncTask which would execute the request in the background and update the UI. A bit more overhead in thread creation/destruction but this is I/O bound anyway. You could make a thread pool if that overhead becomes a bototleneck (though I doubt it would).

如果您的真正的想知道,当你的应用程序不再在前台,您可以使用的onStop / onResume重叠两项活动之间。当从A-> B去,B的onResume将A的的onStop之前调用。因此,可以增加在onResume全局计数器和减少它的onStop。该计数器将成为0,当且仅当没有活动是可见的。我用这个成功地跟踪分析的目的precise访问。它需要对所有的活动一个公共基类,虽然。

If you really want to know when your application is no longer in the foreground, you can use the overlap in onStop/onResume between two activities. When going from A->B, B's onResume will be called before A's onStop. Therefore, you can increment a global counter in onResume and decrement it in onStop. That counter will become 0 if and only if no activities are visible. I used this successfully to track precise visits for analytics purposes. It does require a common base class for all your activities, though.

这篇关于获取的onPause和放大器; onResume喜欢的应用程序/任务级事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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