Is it possible on Android to detect if another app is running? I need to trigger some action in my app when a Map application (like Waze or Google Maps) is running.
You can use ActivityManager class:
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++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Please be aware that this capability was deprecated in API level-21.
An alternative could be getRunningAppProcesses() method of ActivityManager.
Related
I've seen many posts about how to get the running processes on Android, but things changed with Lolipop. Now the list is trunked for non rooted devices.
Every post was about to do this kind of code:
ActivityManager am = (ActivityManager) mContext
.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = this.getActivityManager().getRunningAppProcesses();
List<ActivityManager.RunningTaskInfo> taskInfo = this.getActivityManager().getRunningTasks(Integer.MAX_VALUE);
I tried it and the only thing that is retrieved is your current application.
#Stan was talking about developping an accessibility service here in this post: How to check current running applications in Android?
I checked the API, and what I saw is that you could be informed of application launch with an accessibility service, but not if it's still running afterward. So I don't think this is the good way to do it.
Does anyone have an idea about how to do it?
In Android Lolipop your code will not work as from Android L onward getRunningTask will not work. You have to use getAppRunningProcess.
Here is the code:-
public static String[] getActivePackagesCompat() {
final List<ActivityManager.RunningTaskInfo> taskInfo = mActivityManager.getRunningTasks(1);
final ComponentName componentName = taskInfo.get(0).topActivity;
final String[] activePackages = new String[1];
activePackages[0] = componentName.getPackageName();
return activePackages;
}
public static String[] getActivePackages() {
final Set<String> activePackages = new HashSet<String>();
final List<ActivityManager.RunningAppProcessInfo> processInfos = mActivityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
activePackages.addAll(Arrays.asList(processInfo.pkgList));
}
}
return activePackages.toArray(new String[activePackages.size()]);
}
Time and Name of application opened
final PackageManager pm = getPackageManager();
packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo : packages )
{
String name=packageInfo.getClass().getName();
Toast.makeText(ListActivity.this, ""+ name, 0).show();
}
Try this to get the list of running apps.
ActivityManager activity_manager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
Use getRunningTasks() method from ActivityManager.
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++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Add this permission in the manifest file.
<uses-permission android:name="android.permission.GET_TASKS" ></uses-permission>
I need create a function but I don't know how to get the PID of the app.
// here return -1 but I need PID to kill process
Integer PID1= android.os.Process.getUidForName("com.android.email");
android.os.Process.killProcess(PID1);
try
restartPackage code
ActivityManager aM = (ActivityManager);
getApplicationContext().getSystemService(getApplicationContext().ACTIVITY_SERVICE);
aM.restartPackage("com.android.email");
Kill BackGround Process Code
ActivityManager aM = (ActivityManager)
getApplicationContext().getSystemService(Catalogue.content_holder.getApplicationContext().ACTIVITY_SERVICE);
aM.killBackgroundProcesses("com.android.email");
Here is code which fetches all running Application and check wether email app is already running or not , if it is running then kill that process
ActivityManager manager = (ActivityManager) getApplicationContext.getSystemService(getApplicationContext.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> activityes = ((ActivityManager)manager).getRunningAppProcesses();
for (int iCnt = 0; iCnt < activityes.size(); iCnt++){
System.out.println("APP: "+iCnt +" "+ activityes.get(iCnt).processName);
if (activityes.get(iCnt).processName.contains("com.android.email")){
android.os.Process.sendSignal(activityes.get(iCnt).pid, android.os.Process.SIGNAL_KILL);
android.os.Process.killProcess(activityes.get(i).pid);
//manager.killBackgroundProcesses("com.android.email");
//manager.restartPackage("com.android.email");
System.out.println("Inside if");
}
}
I've a problem with killing third party apps from my application. Here's the code:
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcess.pkgList[0].equalsIgnoreCase("com.adobe.air")) {
Log.v("ACTIVITY FOUND", "" + appProcess.pkgList[0]
+ " - " + appProcess.pid);
activityManager.killBackgroundProcesses("com.adobe.air");
activityManager.restartPackage("com.adobe.air");
android.os.Process.killProcess(appProcess.pid);
}
}
}
Log.v("RUN", "----------------------------------");
And in AndroidManifest I added the permissions android.permission.KILL_BACKGROUND_PROCESSES and android.permission.RESTART_PACKAGES.
In Log I can correctly read the message when the package com.adobe.air is running, but killBackgroundProcesses, restartPackage and KillProcess have no success force closing the app itself. What's wrong?
You can use Process.killProcess(int pid) to kill processes that have
the same UID with your App.
You can use
ActivityManager.killBackgroundProcesses(String packageName),with
KILL_BACKGROUND_PROCESSES permission in your manifest(for API >= 8)
or ActivityManager.restartPackage (String packageName)(for API < 8)
to kill specified process,except of forground process.
I need to create an app that is able to check when other apps are opened or closed ("on resume" or "on pause") on the device. I need to check the time of use of each app.
Can I do it without root access in the device?
try this code
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.android.browser")) {
Toast.makeText(getApplicationContext(), "Browser is running", Toast.LENGTH_LONG).show();
}
}
find the package name using download package name viewer
https://play.google.com/store/apps/details?id=com.gijoon.pkgnameviewer&hl=en