Detect whether process is system process or not - android

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!

Related

Cannot get all running Android processes

I want to get all running processes in my phone. I have tried this code
ActivityManager am = (ActivityManager)
getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
But this code returns only my process.
ActivityManager am = (ActivityManager)this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> listProcess = am.getRunningAppProcesses();
for(RunningAppProcessInfo runningProInfo:listProcess){
Log.i("Running Processes", ":"+runningProInfo.processName);
}
Working code.
Hope this helps..

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

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).

How to get PID from package name?

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

How to get names of background running apps in 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.

Categories

Resources