user1166635
user1166635

Reputation: 2801

How to detect is application been showing?

I have been developing Android application that use Activity with "download" button and Service for executing downloading in the background. And I have following task: to show message about downloading if application is currently displayed. How can I detect it? Is there standard Android OS functions for it? Thank you.

Upvotes: 0

Views: 82

Answers (2)

SpeedBirdNine
SpeedBirdNine

Reputation: 4676

The basic method is to use Broadcast receiver to share information between a service and an application. If your requirement is to update the status/progress of download on the main activity, you can use it.

Here is a simple tutorial about how to share information between a service and an activity. You can update the progress of download. It works for the activity that is currently running.

To check for it as the activity starts, you should override the onCreate, onStart() or onResume() functions.

Also there is a very neat solution presented in another question and i recommend that you should try to use that. It involves extending the Application class to store the information, and use its instance to check for updates. See this

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

You can do this at the activity level, not for the application as a whole. Check out the description of the activity lifecycle. (Also see "Managing the Activity Lifecycle" in the Activities framework topic.) When your activity becomes visible, the framework will call it's onStart() method. When the user can interact with the activity, the framework will call the onResume() method. You can override one of these to know when your activity is showing or actually interacting with the user.

Note that, as described in the documentation, things work a little differently starting in Honeycomb (3.0).

Upvotes: 1

Related Questions