Reputation: 13061
in my app i have to launch another app, only if it is not yet launched.
To launch an app what i do is:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("package.of.app.to.launch");
startActivity(intent);
My problem is that if app is already launched, it will bring the app to the front. How can i launch app only if is not present in active and launched apps? thanks!
Upvotes: 2
Views: 231
Reputation: 13
you can get that which activity is running
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
String currentActivity=taskInfo.get(0).topActivity.getPackageName();
then check
if(currentActivity!=(your activity))
{
**your intent**
}
else{
"app already running"
}
hope it helps. thank you
Upvotes: 0
Reputation: 27748
You can get a list of all running or active apps and then check if the App you want to start is in it.
ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcInfo = activityManager.getRunningAppProcesses();
for(int i = 0; i < runningProcInfo.size(); i++){
if(runningProcInfo.get(i).processName.equals("package.of.app.to.launch")) {
Log.i(TAG, "XXXX is running");
} else {
// RUN THE CODE TO START THE ACTIVITY
}
}
Upvotes: 2
Reputation: 11141
If you want to know whether an app is running or not, you should have a look at getRunningTasks
Also this post might help you.
Upvotes: 0
Reputation: 132992
You can use ActivityManager to get Currently running Applications and to get recent task executed on Device as:
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
// Here you can put your logic for executing new task
}
Upvotes: 0