English 中文(简体)
如何能发现安乐施会的活动是否活跃?
原标题:How Can an Android Application Detect Whether Any of its Activities are Active?
  • 时间:2011-10-26 16:50:26
  •  标签:
  • android

我们有一个信使,定期对数据进行投票,并更新显示。 投票由背景线索进行。 不幸的是,这种背景不断发生,即使我们的用心被抹去,或者当装置被锁锁定时,这导致不必要的网络活动和CPU/批量使用。

因此,我要改变这样的说法,即它将暂停在这些情况下的投票活动:

  • When none of the application s activities are in the foreground.
  • When the device is locked.

如何最容易地发现这种说法是否在这两个国家?

注:该评估有几项活动,因此,我认为这简而言之,只是追踪活动生命周期。 我必须增加每项活动的代码,以追踪我的活动是否在地下。 如果这是我需要做的,我就这样做了,但我希望这样做更为简单。


这些是一些相关的问题,但似乎没有提供很好的答案:

最佳回答

如何最容易地发现这种说法是否在这两个国家?

For the are-we-in-the-foreground issue, increment a reference count in a static data member in onStart() of each activity, and decrement it in onStop(). If onStop() sees 0, stop the polling. If onStart() sees that you re not polling, start polling.

For the is-the-screen-locked issue, don t worry about it. The device will fall asleep once the screen times out and your polling thread will not be running. Besides, I think your activity will be stopped in this case anyway.

BTW,这是以下情形之一:onStop(>> 是正确的答案,而不是onPause(),这样,,你的活动之间的寿命周期交割。

如果这是我需要做的,我就这样做了,但我希望这样做更为简单。

这很简单。

问题回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签