mthakuri
mthakuri

Reputation: 1095

How to check if my application is running in android(not as a service)?

Problem: I have to check if my app is running or not(when a service is already running in background). Based on it I am suppose to either start particular activity or the app.

Thing I tried or come up with but failed I tried to check current running process and based on it tried to decide if app is running or not. Reason for failure: Get app running status as always true since I already had service running in background.

Possible solution but not sure if that possible in android 1. If we can run service by different name(package name) 2. Set some boolean value to true and false on app start and close respectively. But is this a good approach?

EDIT::

Sorry if I wasn't very descriptive earlier. I try to explain you what exactly I'm trying to acheive.

Whenever user click on notification. I have to do any one out of two option after checking if my app is launched(excluding services since they are always running in the background). 1. If my app is already launched just start the required activity. 2. If app is not launched then I have to launch it and then open the required activity.

e.g I get a message arrival notification. If my application is launched I have to show inbox otherwise I have to launch my application and then open the inbox.

Upvotes: 4

Views: 4894

Answers (3)

Dmitri Novikov
Dmitri Novikov

Reputation: 123

Though the question is rather old, I would like to post my own answer, as I spent too much time seeking for the similar problem's solution. My service is working transmitting data gathered by visual application (camera, GPS, etc.). It has to stay on when user closes application, as it needs to transmit data that hasn't been already transmitted to the server when app was on. But it has to switch its working mode to a bit more soft (to save power) (and it will finally close itself when the job is finished, but that's another story. First, I tried to send some command from main activity to this service when it was about to close on user's intent. But this didn't work if I just flicked my app off in the task list (home button long press, or some other on other phones). I found another solution. I have CommonData class for app's common objects. I noticed that if I write something like

CommonData.runObject = new Object();

in main activity's onCreate, this runObject is set to null when app is flicked off.

So, in my service I just need to tell CommonData.runObject != null from CommonData.runObject == null to see if my app's running, or not.

Upvotes: 2

Aerinx
Aerinx

Reputation: 152

I'll try to guess since you're not being clear, as hasan said, if you're running a service, that is your app running, so it could be another app different than the service, when you say app you may be talking about an Activity, and that could be in foreground or background.

In any case, you could set up a static boolean inside the service set to false and then set it to true or false whenever you consider your app to be running based on any state of the application and your own definition on what is a running app. You'll need to override the state where you want to enable it and set the boolean true; and where you want to disable it, override and set to false.

Look here to see the Activity lifecycle, as typically you'll want to measure your application life through Activity life since it is the most visible to the user, however, other components have other lifecycles similar to this that you can use too:

http://developer.android.com/training/basics/activity-lifecycle/index.html

Upvotes: 0

hasanghaforian
hasanghaforian

Reputation: 14042

When your service is working in background,really your App is running!If you want to check any Activity of your App is forground or background,I think that you can use lifecycle methods of Activities and set boolean in Application class or you can get list of running tasks in your service and check is any of your Activity is running:

ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE); 
List<RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE); 
isActivityRunning = false; 
for (int i = 0; i < activitys.size(); i++) { 
    if (...) {
        isActivityRunning = true;
        break;
    }
}

But see this.

Others:

Check if Activity is running from Service

How to determine if one of my activities is in the foreground

discovering if android activity is running

Upvotes: 0

Related Questions