Check if the application is running on Android

I have an application that includes a sound pool. - When you start the application, all sounds are loaded after loading the main screen.

Now I would like to check if the application is running - if it is not running, I would like to restart the application - if it is running, I do not want to restart the application.

Why did I try 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 perfectly, but keeps checking until procInfos.size is less than 0. Until the application gets the process com.me.checkprocess true - is it always false? therefore, he always falls into the first part.

So for example: if the overall process is 30. And my com.me.checkprocess process has been running since the 29th. When the condition is met. Until the 28th process, this is different, and the @ 29th process goes into the true condition.

How to fix this part - I would like to switch to another part only after checking the whole process?

Tell me!

+6
source share
5 answers

I think you can just add

 break; 

below

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

in if

+7
source

Just put a break inside the for loop.

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

It is definitely not clean, but it does what you need.

** EDIT 2

I think it would be easier if you just retrieved 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 } } 
+3
source

This is what you should do.

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

The other listed answers will work, but I agree with c4pone - in such a loop, a gap looks like a bad way to do this, especially if using break means that if you add another condition around your if, there is a chance that break can no longer function when exiting the for loop - in some ways it is similar to the goto operator in other languages

Another alternative to your problem is the following:

 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 in a termination clause, which means that once it is detected, the loop will end.

EDIT:

After the OP comment below, I feel that the solution he wants is the following:

 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 not connected with the exit from the cycle prematurely, since we all thought that we were reading the question, and instead we output the result only once

+2
source
  public static boolean isAppRunning(Context context) { 

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

  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; } 
0
source

All Articles