How can i determine when my application is running?

I have an application that uses a Service to fetch data in the background both while the application is running and when it's not. When it is not running i would like to show a notification when there is new data, but not when the app is running.

How do i determine in my service whether the app is running or not?

Asked by: Jared771 | Posted: 25-01-2022

Answer 1

I think you want to check whether a certain activity is shown. If that is true, I would use the Activity.onCreate() method to set a flag in the application instance, i.e. extend the class Application with a field "uiRunning" and check this field in your service. onDestroy() should be used to unflag the attribute. Don't forget to use your Application class also in the Manifest.xml

Answered by: Oliver805 | Posted: 26-02-2022

Answer 2

I would agree with the use of onCreate()/onDestroy() for a single Activity application, though an Application with multiple activities would be better off using Application.onCreate()/onTerminate() in order to avoid triggering the uiRunning state when switching activities.

Answered by: Dexter706 | Posted: 26-02-2022

Answer 3

This is dead easy. You use a named Mutex.

Put this in the application you want to check:

bool createdNew;
Mutex mutex = new Mutex(true, @"Global\YourAppNameHere", out createdNew);
if (createdNew)
    var app = new YourProcess();;

Put this in the application that checks to make sure the other app is running:

bool createdNew;
Mutex mutex = new Mutex(true, @"Global\YourAppNameHere", out createdNew);
if (createdNew)
    Console.WriteLine("App not running");
} else {
    Console.WriteLine("App is running");

Answered by: Elise990 | Posted: 26-02-2022

Answer 4

Another option is to implement a listener pattern and have your service manage a list of listeners with methods on your service interface for addListener() and removeListener(). Your activity can add itself as a listener after it connects to the service and remove itself onStop() (i.e. when the app is no longer visible to the user or has shutdown completely).

In your service, check the count of listeners. If there are no listeners then you know you should create your notification.

Answered by: Alford957 | Posted: 26-02-2022

Answer 5

Another way to accomplish is to de-couple your data receiver to a 'service' which will always run in the background. you can have your application bind to the service and will display the data fetched by the service.

The problem with having the application in background is that Android will kill the application once it gets too old. Its always better to have such background running application as service rather then activity.

Answered by: Adrian657 | Posted: 26-02-2022

Answer 6

What i did do was to use a flag in my service, that the root activity sets and clears on onStart/onStop. This works pretty well.

Answered by: Kimberly253 | Posted: 26-02-2022

Answer 7

This is how I did it and is working flawlessly with just a few lines of code:

In extended App class:

public static boolean isAppInFront = false;

In you main activity:

public void onStart() {
    MyApplication.isAppInFront = true;

public void onStop() {
    if (isFinishing())
        MyApplication.isAppInFront = false;

Answered by: Joyce625 | Posted: 26-02-2022

Similar questions

android - Determine if a specific activity in an application can be launched

Applications can have any number of launchable activities. I know how to get the list of these activities via PackageManager. Is there a way to determine which activities can be launched via startActivity? For example, the Documents To Go app has different activities that will start Word, Excel, Powerpoint, PDF, etc... I am able to launch all o these just fine. However, it also contains some activ...

api - I cannot determine error in my android application

How can I make android phone download the maps from the wi-fi internet but not from 3G internet? I have set <uses-permission android:name="android.permission.INTERNET"/> but I still see only grid gray lines when I install and start app on actual phone. When i start the app on the emulator it loads flawlessly.

java - How can I determine if my Android application is running in an AVD

This question already has answers here:

java - How to determine that an application in being used by the user (in android)?

I want to record what application is being used by the user at every time. I can write a program to save the processes running on the phone, but I want to know which one is the one that the user is interacting with.

Determine original purchase date of an Android application

Is it possible to use the Android in-app billing API to determine the original purchase date of the application? It looks like making a RESTORE_TRANSACTIONS request may provide that information, but I can't tell from any of the documentation.

time - Determine how long an Android application (not mine) is visible

I'm writing an application that will need to know how long any given application is visible to the user. Is there any system-wide event that my service could receive when an application is opened and when it is no longer visible to the user (onStop())? Or do I have to resort to periodically polling the ActivityManager? The applications Tasker(h...

java - Determine if application is installed on android

On my android phone I launch an standard browser, after enter my URL and press open. I need some ways to determine if I have installed on my phone for example MYAPP, must I write an plugin for my Android browser or may be I must add some java functionality to my web site or maybe something else ? I need some information, articles or something else of how can I do that, every kind of information will...

android determine if activity context or application context

I have an async task that makes an upload and shows a progress dialog. I would like to be able to not show the dialog if it is being called by a service and thus can't show a dialog. The problem is there a way to determine which type of context i have(an activity or an application) ?

android - Determine application first run

I want to determine whether my application being run for the first time, and so it will be hard to pass around it. The preferences or empty file approaches are not going to work because you can simply clear the application data or delete the empty file. Also I want to do it offline so no server checking is going to work either. Maybe it's possible to add some code to the installation of the apk so i...

callback - Android: How can I determine if an application starts with no or low GSM signal?

I have written a piece of Android code that registers a PhoneStateListener and reports the GSM signal strength values when there are changes, through the onSignalStrengthsChanged callback. Everything works fine, but I want to have the initial signal strength before the first change occurs.. Is there a way to find out the GSM signal strength before the end of the onCreate method (see t...

android application memory leaks

I am using kind of results search engine, problem is to remember the searching criteria i made singleton static class which keeps only one instance. In my application there are lots of class level private variables, lots of public static variables, a big util class which contains only static final methods. problem is my application get crash any time any where any spot and interesting thing is crash code a...

Android Application data should not be released by android OS

public class MYApplication extends Application { String property; setter getter } does above code make sure property will not be collectied by android OS if not used for a long period of time.

Android Email application - Is it possible to get all the details which are configured in Email application

Is there any way to get in the code: Email id Password SMTP host SMTP port which the user used to setup his Email account with the Android built in Email application Required because, I am written a MailSender class using JavaMail API there I need to send the mail using the details what user configured in Built in Android Email application.

Android Intent for Twitter application

Is it possible to show a list of applications (with intent.createChooser) that only show me my twitter apps on my phone (so htc peep (htc hero) or twitdroid). I have tried it with intent.settype("application/twitter") but it doesnt find any apps for twitter and only shows my mail apps. Thank you, Wouter

webserver - Retrieve data from a web server for android application

I am developing an application that needs to retrieve some data from a web server. But I have no idea how can this be possible?

Should a legacy Android application be rebuilt using SDK 2.1?

I have an Android application that uses the well known Strategies for Legacy Applications. It is build with the Android SDK 2.0 with manifest settings minSdkVersion="3" (API 1.5) and targetSdkVersion="5" (2.0). Question1: Since maxSdkVersion is not specified, the application should be...

android - Intent to pick an installed application slow in my activity, why?

If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications. I needed this same functionality in my application so I copied the intent from the launcher source: Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pic...

web services - How to store data accessible to both a web site and android application?

I’m developing an Android and web application that will function as a service (use the same data). My question is how should the data be stored to allow for both the web and the android application to have access to the same set of data? Should the android application connect to the sites MySQL server to store/access data? If so how do I allow someone to access the data when they are not in ...

Android 1.6 SMS (older application code)

I have HTC Tattoo with Android 1.6. I have maed a Java program for SMS sending. I got the source on the Internet, I think, versions before 1.6. The problem is: SMSs are sent twice. What is a possible cause for this problem? If possible, please simply post sample code what works OK.

How to close Android application?

I want to close my application, so that it no longer runs in the background. How to do that? Is this good practice on Android platform? If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android