确定是否在前台应用程序 - 这是令人难以接受的? [英] Determine if application on foreground - is that frowned upon?

查看:250
本文介绍了确定是否在前台应用程序 - 这是令人难以接受的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有,为什么检测,如果应用程序是对前景的原因很多。 例如 - 作为触发,GCM / C2DM推送通知 - 大量的应用程序将有充分的理由实施不同的行为,当应用程序是前台和后台。 其他原因可能是 - shotting向下服务耗费precious资源,如查询在后台任务服务器例如

只是要清楚:的定义(在我看来)的后台程序是
其中没有它的活动被调用的 ONSTART()的方法,并没有援引尚的onStop()方法的应用。 这是因为活动是可见的,以它的生命周期中的用户只在那个时候。

从另一方面 -

  • 好像谷歌不希望应用程序能够响应home键(这不是API的一部分)

  • 反应的 onBack pressed()上的根/主活动为指示剂离开活动肯定不是好主意(因为大量的使用Home键,而不是后退按钮的用户)

  • 还有就是API允许确定应用程序是前台在没有方法(根据我的定义。)

如果我没有错过一些东西在API中,这真是如此 - 为什么没有为什么决定很容易,如果应用程序是前景或不???? !!!!

什么,我知道我可以做以确定应用程序的前景在这个线程描述 - <一个href="http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo">How以检测当一个Android应用程序转到后台,回到前台

但@Emil说 - 这是需要特别的许可,或者需要一些棘手的逻辑的它很快成为问题保持,它闻起来像不好的做法(虽然这是我在做什么,现在,因为我没有更好的主意...)

我的问题基本上都是

  • 有没有从充足的理由没有这样的API方法?

  • 是考虑到如果应用前景与否是一个不错的办法?

  • 有没有知道,如果应用前景有任何其他方式?

解决方案
  

是考虑到如果应用前景与否是一个不错的办法?

取前景与背景的考虑是合理的。

  

有没有知道,如果应用程序是前景或没有任何其他的办法吗?

您可以大致划分方案为这个分为两组:

  1. 要在在前台/后台状态的变化立即采取行动案例

  2. 在那里一些其他的事件发生( AlarmManager 报警,传入系统广播等),在这一点上,你要采取基于是否不同的操作案例或者不是你是在前台

在前者的情况下, onUserLeaveHint()是您最可靠的简单的选择。我不能保证它会涵盖所有情况,但它应该处理的HOME的情况下,例如。也欢迎您来维持开工活动的引用计数的静态数据成员,并尝试使用它来代替。

在后一种情况下,一个有序广播可能是有用的。

There are lots of reasons why detecting if application is on foreground. for example - as a trigger to GCM/C2DM push notification - lot's of apps would have good reason implementing different behavior when app is foreground and background. Other reason could be - shotting down services consuming precious resources, such as querying servers in background task for instance.

Just to be clear: the definition (as I see it) for background app is:
application which none of it activities are invoked the onStart() method, and did not invoke yet the onStop() method. that's because activity is visible to the user in it life cycle only at that time.

From the other hand -

  • seems like Google don't want application to react to the home button (it's not part of the API)

  • reacting to the onBackPressed() on the "root / main" activity as indicator for leaving Activity certainly not good idea (because lots of users using the home button, and not the back button)

  • there is no method in the API allowing determine if app is foreground (according to my definition..)

if I didn't miss something in the API, and it's really the case - Why there is no why to determine easily if the application is foreground or not????!!!!

what I know I can do to determine if the application is foreground is described in this thread - How to detect when an Android app goes to the background and come back to the foreground

but as @Emil saying - it's requiring special permission, or requiring some tricky logic's which very fast becoming problematic to maintain, and it smells like bad approach (although that's what I'm doing for now, because I don't have better idea...)

my questions basically are:

  • Is there no such API method from good reason?

  • Is taking into account if application is foreground or not is a bad approach?

  • Is there any other way to know if application is foreground or not?

解决方案

is taking into account if application is foreground or not is a bad approach?

Taking foreground versus background into account is reasonable.

is there any other way to know if application is foreground or not?

You can roughly divide the scenarios for this into two groups:

  1. Cases where you want to take an action immediately upon a change in the foreground/background status

  2. Cases where some other event occurs (AlarmManager alarm, incoming system broadcast, etc.), and at that point you want to take different actions based upon whether or not you are in the foreground

In the former case, onUserLeaveHint() is your most reliable simple option. I cannot guarantee that it will cover all cases, but it should handle the HOME scenario, for example. You are also welcome to maintain a reference count of started activities in a static data member and try to use it instead.

In the latter case, an ordered broadcast can be useful.

这篇关于确定是否在前台应用程序 - 这是令人难以接受的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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