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!
I think you can just add
break;
below
Log.e("Result", "App is running - Doesn't need to reload");
in if
statement
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With