Android:应用程序全局 onPause() 和 onResume()?

发布于 2024-08-18 13:11:04 字数 281 浏览 4 评论 0原文

是否有类似应用程序全局 onPause()onResume() 的东西?

我的主要活动监听 GPS 修复,我希望在切换到另一个屏幕/活动时继续工作。因此,我无法在活动的 onPause() 中取消注册我的 LocationListener。但是,我仍然想在切换到另一个应用程序时注销我的 GPS 侦听器(以便节省电池),并在返回我的应用程序时将其重新打开,无论用户当前处于哪个屏幕/活动中。

有什么想法吗?

Is there something like an application global onPause() and onResume()?

My main activity listens for GPS fixes, which I want to continue working when switching to another screen/activity. Therefor I cannot unregister my LocationListener in the activity's onPause(). However I still want to unregister my GPS listener when switching to another application (so save battery) and turning it back on when returning to my application, regardless what screen/activity the user is currently in.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-08-25 13:11:04

有没有类似应用程序的东西
全局 onPause() 和 onResume()?

不,抱歉。

我的主要活动是监听 GPS
修复,我想继续
切换到另一个时工作
屏幕/活动。因此我不能
取消注册我的 LocationListener
活动的 onPause()。然而我还是
想要取消注册我的 GPS 侦听器
当切换到另一个应用程序时
(所以节省电池)并将其转回来
返回我的申请时,
无论什么屏幕/活动
用户当前处于。

这是一种可能的方法:

步骤 #1:将 LocationListener 逻辑移至 Service 中,活动通过本地绑定模式或其他方式连接到该服务。还至少有一个服务调用 startService(),因此 unbindService() 不会导致 Service 消失(假设您'使用本地绑定模式)。

步骤#2:让活动在 onPause()onResume() 期间调用服务。

步骤#3:让服务维护未完成活动的引用计数。

步骤#4:当引用计数达到零时,安排服务通过 Timer 和 TimerTask 唤醒。另外,如果引用计数增加,请取消任何此类未完成的 TimerTask。

步骤#5:让TimerTask 在执行时关闭GPS。

实际情况是,只有在一定程度的不活动后,您才会释放 GPS。这种不活动可能是出于任何原因。

Is there something like an application
global onPause() and onResume()?

No, sorry.

My main activity listens for GPS
fixes, which I want to continue
working when switching to another
screen/activity. Therefor I cannot
unregister my LocationListener in the
activity's onPause(). However I still
want to unregister my GPS listener
when switching to another application
(so save battery) and turning it back
on when returning to my application,
regardless what screen/activity the
user is currently in.

Here's one possible approach:

Step #1: Move the LocationListener logic into a Service, which the activities connect to via the local binding pattern or something. Also have at least one service call startService(), so an unbindService() won't cause the Service to go away (assuming you're using the local binding pattern).

Step #2: Have the activities call into the service during onPause() and onResume().

Step #3: Have the service maintain a reference count of outstanding activities.

Step #4: When the reference count reaches zero, have the service arrange to get woken up via a Timer and TimerTask. Also, cancel any such outstanding TimerTask if the reference count gets incremented.

Step #5: Have the TimerTask shut down GPS if it ever gets executed.

The net is that you will only release GPS after such-and-so amount of inactivity. That inactivity could be for any reason.

梦过后 2024-08-25 13:11:04

使用 ActivityLifecycleCallbacks。在您的应用程序中注册它:

registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit
    override fun onActivityStarted(activity: Activity) = Unit
    override fun onActivityResumed(activity: Activity) {
        Log.d(TAG, "Activity resumed: $activity")
    }
    override fun onActivityPaused(activity: Activity) {
        Log.d(TAG, "Activity paused: $activity")
    }
    override fun onActivityStopped(activity: Activity) = Unit
    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
    override fun onActivityDestroyed(activity: Activity) = Unit
})

Use ActivityLifecycleCallbacks. Register it in your Application:

registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) = Unit
    override fun onActivityStarted(activity: Activity) = Unit
    override fun onActivityResumed(activity: Activity) {
        Log.d(TAG, "Activity resumed: $activity")
    }
    override fun onActivityPaused(activity: Activity) {
        Log.d(TAG, "Activity paused: $activity")
    }
    override fun onActivityStopped(activity: Activity) = Unit
    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
    override fun onActivityDestroyed(activity: Activity) = Unit
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文