How to get names of background running apps in android? - android

For example, I have "music" and " speech recorder" run in my AVD background. I want to make a AlertDialog that can show the names " music, speech recorder". Who can give me some codes? Thank you.

this may help you...
public static HashSet<String> getRunningApps(Context context) {
final HashSet<String> hashSet = new HashSet<String>();
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final PackageManager packageManager = context.getPackageManager();
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo runningTaskInfo : runningTasks) {
String packageName = runningTaskInfo.baseActivity.getPackageName();
try {
String appName = packageManager.getApplicationInfo(packageName, 0).loadLabel(packageManager).toString();
hashSet.add(appName);
} catch (NameNotFoundException exception) {
// handle Exception
}
}
return hashSet;
}

You can list all processes with the followings lines:
ActivityManager localActivityManager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
List RunningServiceInfo services = localActivityManager .getRunningServices(100);
And from RunningServiceInfo you can get details for the process.
Another way is using getRunningAppProcesses.

Related

Android: get active app names in active session

I've tried to get all names of applications which the user opened in a session (session start with unlocking phone and ends with turning off the screen).
As first I've tried to get the current app in the foreground. I use a service in the background and tried different code:
AndroidManifest.xml:
<uses-permission android:name="android.permission.GET_TASKS"/>
TaskListenerService.java:
ActivityManager am = (ActivityManager) TaskListenerService.this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
Toast.makeText(this, "name foregroundTaskInfo: " + foregroundTaskPackageName, Toast.LENGTH_SHORT).show();
PackageManager pm = TaskListenerService.this.getPackageManager();
PackageInfo foregroundAppPackageInfo = null;
try {
foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
Toast.makeText(this, "name foregroundTaskAppName: " + foregroundTaskAppName, Toast.LENGTH_SHORT).show();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
It works fine in my own app, but if another app is in the foreground I get as package just "com.sec.android.app.launcher" ("TouchWiz-Start" as foregroundTaskAppName).
I also tried this code in TaskListenerService.java with the same result:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> taskInfo = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo task:taskInfo) {
if (task.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
Toast.makeText(this, "name: " + task.processName, Toast.LENGTH_SHORT).show();
}
}
And this code...
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
Toast.makeText(this, "packageName: " + componentInfo.getPackageName(), Toast.LENGTH_LONG).show();
My Questions:
Is there another way to resolve my problem? Maybe with another
approach to get all apps in a session?
Is there another solution without using GET_TASKS? (deprecated)

Get Running Applications on a non rooted Android

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()]);
}

I want to create an application that shows all the applications that were launched

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>

How to get 'process name' of appllications, if it is different from 'package name'?

Here is my scenario,
Suppose I have package name of some application, and presently I’m finding whether the application is running by passing the "packagename" to following method
boolean isNamedProcessRunning(String packageName){
if (packageName == null)
return false;
ActivityManager manager =
(ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
Log.e("", "----> processname "+process.processName);
if (packageName.equals(process.processName))
{
return true;
}
}
return false;
}
I know, by default android takes the package name as the process name. But if process property in application tag is defined in manifest file android:process="com.example.newprocessname" then the application will run with this name "com.example.newprocessname".
How to handle this scenario?
Try this
boolean isNamedProcessRunning(String packageName){
if (packageName == null)
return false;
ActivityManager manager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
for (RunningAppProcessInfo process : processes)
{
for(int i = 0;i < process.pkgList.length; i++){
Log.e("", "----> pkg name "+ i + process.pkgList[i]);
if(process.pkgList[i].equals(packageName))
return true;
}
}
return false;
}
Use :
String arr[] = process.pkgList;
then compare the package name with arr items. It will give all packages that have been loaded into the process(As per doc).

Detect whether process is system process or not

I am using this method to get running processes
private void getRunningProcess() {
{PackageManager pm=getActivity().getPackageManager();
final ActivityManager manager = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
if (runningProcesses != null && runningProcesses.size() > 0) {
setListAdapter(new ListAdapter(getActivity(), runningProcesses));
}
}
I am getting all processes but I want to show only non-system processes.
Any help will be appreciated.
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
try it!

Categories

Resources