TheDevMan
TheDevMan

Reputation: 5954

Check if my App is running in Android

I have an application which involves sound pool - On start of the application all the sounds are loaded after which main screen is loaded.

Now, I would like to check if the application is running - if is not running I would like to reload the application - if it is running I don't want reload the application.

For which I tried the following:

 ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
 List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
 for(int i = 0; i < procInfos.size(); i++){
   if(procInfos.get(i).processName.equals("com.me.checkprocess"))
   {
    Log.e("Result", "App is running - Doesn't need to reload");
   }
   else
   {
     Log.e("Result", "App is not running - Needs to reload);
  }
}

This gives the results out perfectly but keeps on checking until procInfos.size is lesser that 0. Until the app gets process com.me.checkprocess true - it is always false? so it always gets into the else part first.

So for example: If the total process is 30. And my process com.me.checkprocess is running @29th. When execute the condition. Until 28th process it is else and @29th process it gets into the condition true.

How to fix this part - I would like to get to else part only after total process is checked?

Let me know!

Upvotes: 6

Views: 9622

Answers (5)

Ashish Soni
Ashish Soni

Reputation: 296

 public static boolean isAppRunning(Context context) {

// check with the first task(task in the foreground) // in the returned list of tasks

  ActivityManager activityManager = (ActivityManager)
 context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services =
activityManager.getRunningTasks(Integer.MAX_VALUE);
 if
 (services.get(0).topActivity.getPackageName().toString().equalsIgnoreCase(context.getPackageName().toString()))
 {
 return true;
 }
 return false;
 }

Upvotes: 0

c4pone
c4pone

Reputation: 797

Just put an break inside the for-loop.

 for(...){
    if(){
       break;
    } 
 }

This is definitely not clean, but does the thing you want.

**EDIT 2

I think it would be easier if you just extract this task in a method.

public boolean isProcessRunning(String process) {
    ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++){
        if (procInfos.get(i).processName.equals(process)) {
            return true;
        }  
    }
    return false;
}

public static void main(String[] args) {
    if (isProcessRunning("my_process")) {
        // Do what you need
    } else {
        // restart your app
    }
}

Upvotes: 3

Matt Taylor
Matt Taylor

Reputation: 3408

The other listed answers will work, but I agree with c4pone - in a loop such as this, break seems like a bad way to do it - especially as using a break means that if you add another condition around your if statement, there's a chance that the break may no longer function in exiting the for loop - in some ways it's analogous to a goto statement in other languages

Another alternative to your problem is this:

for(int i = 0; i < procInfos.size(); i++){
   if(procInfos.get(i).processName.equals("com.me.checkprocess"))
   {
        Log.e("Result", "App is running - Doesn't need to reload");
        i = procInfos.size();
   }
   else
   {
        Log.e("Result", "App is not running - Needs to reload);
   }
}

Which will also break the loop, as it sets your counter variable to the termination clause, meaning once it's found, the loop will exit.

EDIT:

After the OP's comment below, I have a feeling that the solution he wants is this:

boolean found = false;
for(int i = 0; i < procInfos.size(); i++){
   if(procInfos.get(i).processName.equals("com.me.checkprocess"))
   {
       found = true;
   }
}

if(found)
{
    Log.e("Result", "App is running - Doesn't need to reload");
    i = procInfos.size();
}
else
{
    Log.e("Result", "App is not running - Needs to reload);
}

Which is nothing to do with exiting the loop prematurely, as all of us thought from reading the question, but is instead to only output the result once

Upvotes: 2

nithinreddy
nithinreddy

Reputation: 6197

This is what you should be doing.

   if(procInfos.get(i).processName.equals("com.me.checkprocess"))
   {
    Log.e("Result", "App is running - Doesn't need to reload");
    break;
   }

Upvotes: 2

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

I think you can just add

break;

below

Log.e("Result", "App is running - Doesn't need to reload");

in if statement

Upvotes: 7

Related Questions