How to know Specificaly Name of the running apps in android? - android

I get a list of apps which are running on device correctly:
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(99);
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
But with this I get complete package name like:
com.google.process.location, com.whatsapp
I also used split function
for (String aa1 : risky_arr) {
String[] splits = aa1.split(" ");
String aa2 = (splits[1].substring(0, splits[1].length()-1));
hashSet.add(aa2);
}
But it also shortens the length like I said above.
So my problem is, how can I get only app name like Whatsapp, location, paisa, swipe etc.

Use following code to get app name:
ResolveInfo packageInfo = mApps.get(position);
PackageManager pm = getActivity().getPackageManager();
appName = new String(packageInfo.loadLabel(pm).toString());

Related

Fetch list of running activities in Android (from Lollipop devices onwards)

I am working on a personal project where I need to fetch a list of all running packages in android.
I have been able to fetch a list of running applications in pre-lollipop devices using this code.
ActivityManager m = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> runningTaskInfoList = m.getRunningTasks(10);
Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
while(itr.hasNext())
{
RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
int id = runningTaskInfo.id;
CharSequence desc= runningTaskInfo.description;
String topActivity = runningTaskInfo.topActivity.getShortClassName();
int numOfActivities = runningTaskInfo.numActivities;
}
But it doesn't work with Lollipop devices. Can someone please assist me with this?
Please read the javadoc for getRunningTasks(). Basically, you can't do this any more because it's seen as a security problem for the end user.

Get the RAM usage of a process by using its pid android [duplicate]

i want to log ram usage of a given application at a given time rate. i wrote the code to get the full memory value used but do not know how to get the memory usage of a given PID.
please help me out
this is the code i used to get the memory
ActivityManager localActivityManager = (ActivityManager)getSystemService("activity");
ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
localActivityManager.getMemoryInfo(localMemoryInfo);
Log.i("",String.valueOf(localMemoryInfo.availMem));
i think i can get it using public MemoryInfo[] getProcessMemoryInfo (int[] pids), but do not know how to code for it since im a android beginer
ActivityManager localActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); // use Context.ACTIVITY_SERVICE not the literal "activity"
List<ActivityManager.RunningAppProcessInfo> procsInfo = localActivityManager.getRunningAppProcesses();
int[] pids = new int[procsInfo.size()];
for (int i = 0; i < procsInfo.size(); i++) {
ActivityManager.RunningAppProcessInfo info = procsInfo.get(i);
pids[i] = info.pid;
}
Debug.MemoryInfo[] procsMemInfo = localActivityManager.getProcessMemoryInfo(pids);
// now walk the procsMemInfo array
If you schedule a recurring timer to periodically re-query for running pids and query for memory info you can use timestamps to compute the memory usage over time.

Android Running Task

How to get the list of applications which are currently running in android system and how to update it on the layout with their icon image.
And also help me to get all recent used application list and how to close one existing running application.
ActivityManager actvityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for (int i = 0; i < procInfos.size(); i++) {
String st = procInfos.get(i).processName + " " + String.valueOf(procInfos.get(i).processName.length());
//How to update it on layout in list format
}
Use custom listview.
create an array for app names and another for images(icons)
Then use custom listview to display it.
Here's a demo of custom listview

Launch most recent app

I'm working on a productivity app and I would like to be able to launch the users most recent app when a gesture is detected, however, I can't figure out why this code below isn't launching my most recent app.
ActivityManager m = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
RecentTaskInfo task = m.getRecentTasks(1, 0).get(0);
startActivity(task.baseIntent);
I also have this permission in my manifest
android.permission.GET_TASKS
Thank you for any help as to why this isn't working
RecentTaskInfo task = m.getRecentTasks(1, 0).get(0);
Since you are setting max number of results to 1, you are getting your own task with get(0). To get the result you are looking for, try setting the max # of results to 2 and use the second task from the returned list:
ActivityManager m = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
RecentTaskInfo task = m.getRecentTasks(2, 0).get(1);
startActivity(task.baseIntent);
Below code works for me to get recent 30 apps:
private static final int MAX_TASKS = 30;
final ActivityManager am = (ActivityManager)
mContext.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTasks =
am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);

android get process name inside BroadcastReceiver

I'm stuck in this trouble and I can't figure it out. I googled a lot, but nothing gave me an answer.
I explain:
I have a BroadCastReceiver which runs in a different process (android:process=":anotherProcess")
I want to get this process name inside my BroadcastReceiver.
In this case for example, it could be: com.my.package:anotherProcess
Is it possible? How can I do it?
I've found a way. For anyone who will need this:
int id = Process.myPid();
String myProcessName =context.getPackageName();
ActivityManager actvityManager = (ActivityManager)context.getSystemService( context.ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(RunningAppProcessInfo procInfo : procInfos) {
if (id == procInfo.pid)
{
myProcessName = procInfo.processName;
}
}

Categories

Resources