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!
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
}
}
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
}
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.
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
Related
I know we can detect the installed application but is there any way to detect that which application is in use or which is in idle/closed state?
For example, if I open the Whatsapp, it can detect the WhatsApp and Toast me a message "using WhatsApp".
You cannot detect an App launch in Android, but you can get the list of currently open apps and check if the app you're looking for is open or not using the following code:
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")) {
// Do your stuff here.
}
}
You can also check if the app is running in the foreground using this method
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager) ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}
this response is from this post :
How to get the list of running applications?
I need best solution. When I received a push notification, I want to know if my app is open or not, because if my application is open when the user onClick the notification I want to call specific method, but if the application is close I want to open the application.
Basically what you need is way to check if one of your activity is in Foreground. May be you should check this How to determine if one of my activities is in the foreground
//Use this method to check your app is running or not
public boolean isAppRunning(){
String packageName="Your package name";
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfoslist = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfoslist.size(); i++)
{
if(procInfoslist.get(i).processName.equals(packageName))
{
return true;
}
}
return false;
}
`
//Use this method to check any activity is running or not
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
I want to develop a background service in android which can notify the user whenever the camera app is launched.This is my code to filter the recently running services. With the help of this code I am still not able to detect the current top activity of the stack which helps me find out if the specific application is launched.
Service.java
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
if(recentTasks.get(i).topActivity.getPackageName().contains("camera") || recentTasks.get(i).topActivity.getPackageName().contains("Camera"))
{
Toast.makeText(getApplicationContext(), "Camera open", Toast.LENGTH_SHORT).show();
break;
}
}
I have a button in a service where the user clicks on it the recent apps dialog will appear, but I don't know a proper way to achieve that, can anyone help?
ActivityManager has method getrunningtasks() .
ActivityManager activity_manager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
then call the getRunningtasks() to get a list of the current Running tasks :
List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++) {
Log.d("Executed app",
"Application executed : "
+ recentTasks.get(i).baseActivity.toShortString()
+ "\t\t ID: " + recentTasks.get(i).id + "");
}
and please add the permission to get the running tasks in your manifest file like the following :
<uses-permission android:name="android.permission.GET_TASKS"/>
Hope that helps .
Recent apps can be opened using the TOGGLE_RECENTS Intent.
Intent intent = new Intent ("com.android.systemui.recent.action.TOGGLE_RECENTS");
intent.setComponent (new ComponentName ("com.android.systemui", "com.android.systemui.recent.RecentsActivity");
startActivity (intent);
You can make your own dialog to show recent apps
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
then you can get all running apps with
getRunningTasks
I have an service running on Android and I need to know if is any application on focus or if the "desktop" (home screen) is in focus. I don't know if this is the proper word to refer to the home screen of the phone. How can I know if this is in focus or some other application?
Inside the service I have this code to get the running tasks:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
How can I know from componentInfo if this is desktop or no? On emulator the componentInfo.getPackageName() returns com.android.launcher but in a Galaxy S1 (I tested only in this phone) returns something else.
There is any other way to do this?
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName currentTask = taskInfo.get(0).topActivity;
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_HOME);
mPackageManager = getPackageManager();
List<ResolveInfo> appList = mPackageManager.queryIntentActivities(mainIntent, 0);
for(int i = 0; i < appList.size(); i++) {
ResolveInfo apl = appList.get(i);
if(currentTask.getPackageName().equals(apl.activityInfo.packageName)) {
Log.e(TAG, "ONHOMESCREEN");
}
}
And in AndroidManifest.xml you need permission
<uses-permission android:name="android.permission.GET_TASKS"/>
Try this function,
public boolean isUserIsOnHomeScreen()
{
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
if(process.pkgList[0].equalsIgnoreCase("com.android.launcher"))
{
return true;
}
else
{
return false;
}
}
return false;
}
If you want to get control when "the home screen is in focus", implement a home screen.