android get process name inside BroadcastReceiver - android

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;
}
}

Related

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.

How to know Specificaly Name of the running apps in 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());

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 actual allocated RAM size

when I open new activities, I need to know my actual memory allocation (app can have huge memory allocation and I need to test real numbers). Is there any API for that?
Thanks
Edit code is not running... there is error on last line:
ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
if(procInfos.get(i).processName.equals("cz.process.a")) {
int field[]=new int []{procInfos.get(i).pid};
android.os.Debug.MemoryInfo[] info = actvityManager.getProcessMemoryInfo(field);
Still not solved. There was someone, who was an expert. But he offered me a code and wasn't able to describe it. He suggested code above, but on the last line there is a critical error.
I believe Debug and Debug.MemoryInfo classes can help you.

What i am doing wrong to get the UID

I saw similar types of posts here.But i am not getting this right.To get the UID of running process i wrote
ActivityManager mgr = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processes = mgr.getRunningAppProcesses();
String text = "All Process:\n";
for (int i = 1; i <= processes.size(); i++)
{
String s;
s = processes.get(i - 1).processName.toString();
text += "Process:" + i + s + ":UID:" + android.os.Process.getUidForName(s) + "\n";
}
But after completion of loop what i am getting in the string text is all UID value as -1.I put GET_TASKS permission in manifest file.Why i am not getting the UID.Please help.I need this UID to kill the process.
To kill the process i used killBackgroundProcess Method of ActivityManager.It needs package name not the UID
Please see this answer by #seanhodges for reference.
Reading the whole thread might be helpful too.

Categories

Resources