What i am doing wrong to get the UID - android

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.

Related

Lollipop: Get top activity name

How to get top activity name in Lollipop?
((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).
getRunningTasks(1).get(0).topActivity
is not longer available for Lollipop:
* #deprecated As of {#link android.os.Build.VERSION_CODES#LOLLIPOP}, this method
* is no longer available to third party
* applications: the introduction of document-centric recents means
* it can leak person information to the caller. For backwards compatibility,
* it will still retu rn a small subset of its data: at least the caller's
* own tasks, and possibly some other tasks
* such as home that are known to not be sensitive.
Calling from onResume in MyActivity
MyApplication.getInstance().saveCurentActivity(MyActivity.this)
saveCurentActivity(Activity a) {
this.activityName = a.getClass().getSimpleName();
}
is no good idea, because MyApplication can be destroyed on error (for example, NPE).
This code is working for me.
ActivityManager activityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = activityManager.getRunningTasks(1);
String currentTopActivity = taskInfo.get(0).topActivity.getClassName();
It will give TopActivity.
This worked for me:
long end = System.currentTimeMillis();
long INTERVAL=2000;
long begin = end - INTERVAL;
if (Build.VERSION.SDK_INT >= 21) {
UsageStatsManager us = (UsageStatsManager) MyServices.this.getSystemService(Context.USAGE_STATS_SERVICE);
UsageEvents usageEvents = us.queryEvents(begin, end);
while (usageEvents.hasNextEvent()) {
UsageEvents.Event event = new UsageEvents.Event();
usageEvents.getNextEvent(event);
if (event.getEventType() == UsageEvents.Event.MOVE_TO_FOREGROUND) {
Log.e("event", "timestamp : " + event.getTimeStamp());
Log.e("event", "package name : " + event.getPackageName());
Log.e("event", "Class name : " + event.getClassName());
//You can find activity name as Class name
}
}
}
I'm not familiar with the original API you are using, but you can track Activity life-cycle changes in your app, using an ActivityLifecycleCallbacks that could be registered via Application.registerActivityLifecycleCallbacks. It would get notified whenever an Activity is created, started, resumed, etc. You might be able to track the current Activity using the onActivityResumed and onActivityPaused hooks.

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.

Get running Apps(Internal and External) with icons

Currently, I'm working on to get a list of all running applications. I've been able to do this in the following way,
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(mInfo);
List<RunningAppProcessInfo> listOfRunningProcess = activityManager
.getRunningAppProcesses();
Log.d(TAG, "XXSize: " + listOfRunningProcess.size());
l1 = findViewById(R.id.Layout1);
for (RunningAppProcessInfo runningAppProcessInfo : listOfRunningProcess) {
if (runningAppProcessInfo.uid > 1026) {
uID = runningAppProcessInfo.uid;
Log.d(TAG, "ANS " + runningAppProcessInfo.processName + " Id :"
+ runningAppProcessInfo.pid + " UID: " + uID);
}
}
It gives a list of all running applications. Now I want to differentiate these applications on the basis of whether they are Internal or External apps. Is there any way to separate out internal and external apps.
Here is the thread which checks the whether the app is system app or not.
so that if you get the list of apps then you can check the apps whether the apps are system apps or not using the above thread. so that you can separate the apps.

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

Android task killer

Im trying to write a simple task killer. I know I shouldnt kill tasks in Android, but Im eager to try something like this. I have the following code:
List<RunningAppProcessInfo> procInfo = activityManager.getRunningAppProcesses();
for (int i = 0; i < procInfo.size(); i++) {
Log.v("proces " + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
//First I display all processes into the log
}
for (int i = 0; i < procInfo.size(); i++) {
RunningAppProcessInfo process = procInfo.get(i);
int importance = process.importance;
int pid = process.pid;
String name = process.processName;
if (name.equals("manager.main")) {
//I dont want to kill this application
continue;
}
if (importance == RunningAppProcessInfo.IMPORTANCE_SERVICE) {
//From what I have read about importances at android developers, I asume that I can safely kill everithing except for services, am I right?
Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL NOT KILL");
continue;
}
Log.v("manager","task " + name + " pid: " + pid + " has importance: " + importance + " WILL KILL");
android.os.Process.killProcess(procInfo.get(i).pid);
}
procInfo = activityManager.getRunningAppProcesses();
//I get a new list with running tasks
for (int i = 0; i < procInfo.size(); i++) {
Log.v("proces after killings" + i,procInfo.get(i).processName + " pid:" + procInfo.get(i).pid + " importance: " + procInfo.get(i).importance + " reason: " + procInfo.get(i).importanceReasonCode);
}
My problem here is that when I perform this code, I first get the list of all tasks, thats ok. Than I see in the log several lines of:
Sending signal. pid: (processId) SIG: 9
I asume thats a signal to die. But at the end of my code, when I display all running processes, the list is the same as before, no task has been killed. Any ide why? Thank you!
You can't kill other tasks that way because the Kernel will enforce permissions. Try this instead:
ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
activityManager.restartPackage(packageName);
you will need the following permissions in the manifest:
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
EDIT:
Actually restartPackage is deprecated. use killBackgroundProcesses() instead!
To achieve best result form killProcess , try calling killProcess many times.as
for (int i = 0; i < 10; i++)
{
android.os.Process.killProcess(procInfo.get(i).pid);
}
NOTE:
version 2.2
- killBackgroundProcesses : " This is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed."
ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
am.killBackgroundProcesses("com.est.testetst");
Required Permissions:
android.Manifest.permission.KILL_BACKGROUND_PROCESSES
version 2.1
- restartPackage -" This method is deprecated. (works for version less than 2.2.
This is now just a wrapper for killBackgroundProcesses(String); the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc."
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
am.restartPackage(getPackageName());
Required Permissions:
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
Fortunately, killProcess() will only work for you to kill your own processes. You can tell this by reading the documentation.

Categories

Resources