Click here to Skip to main content
15,887,975 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to show the list of running processes with the help of android program.But it's not showing anything.What to do?? The code is given below:

MainActivity.java:

package com.sysinfo.procinfo;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

/**
* Starting point of the app. This class will show the list of currently running
* process.
*

* @version 1.0 29-01-2015
*
*/
public class MainActivity extends Activity {

private ListView mProcessListView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

loadProcessInfo();
}

private void initViews() {

mProcessListView = (ListView) findViewById(R.id.processListView);
}

/**
* Get the list of currently running process.
*/
private void loadProcessInfo() {

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<runningappprocessinfo> runningProcesses = manager
.getRunningAppProcesses();

if (runningProcesses != null && runningProcesses.size() > 0) {
mProcessListView
.setAdapter(new ListAdapter(this, runningProcesses));
} else {
Toast.makeText(getApplicationContext(),
"No running process found.", Toast.LENGTH_LONG).show();
}
}
}

ListAdapter.java

package com.sysinfo.procinfo;

import java.util.List;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
* The adapter to handle the data in list.

* @version 1.0 29-01-2015
*
*/
public class ListAdapter extends ArrayAdapter<runningappprocessinfo> {

private Context mContext = null;
private List<runningappprocessinfo> mProcessList = null;

public ListAdapter(Context context, List<runningappprocessinfo> values) {

super(context, R.layout.list_item_process, values);

this.mContext = context;
this.mProcessList = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_item_process, parent,
false);

TextView appName = (TextView) rowView.findViewById(R.id.processName);
appName.setText(mProcessList.get(position).processName);

return rowView;
}

}

activity_process_info.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProcessorInfoActivity" >

<textview>
android:id="@+id/View2"
android:layout_width="wrap_removed"
android:layout_height="wrap_removed"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:lineSpacingExtra="10dp"
android:text="Processor Info."
android:textSize="30dp"
android:textStyle="bold" />

<listview> android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/View2"
android:layout_marginLeft="38dp" >


It is compiling the code and when running on emulator,it shows only the heading Process Info and rest of the page blank.Where my code is lacking?
Posted
Updated 16-Dec-20 15:33pm
v4
Comments
Richard MacCutchan 10-Mar-15 12:18pm    
It is no good just dumping your code (unformatted) and expecting people to tell you what is wrong. Please edit your question, format the code properly with <pre> tags, and explain exactly where it is going wrong and why.
Member 11505839 10-Mar-15 14:16pm    
@Richard..made changes plzz see!
David Crow 20-Feb-18 12:31pm    
Does the getView() method get called? Do you need to override getCount() in your list adapter?

Quote:
Android 5.0+ killed getRunningTasks(int) and getRunningAppProcesses(). Both of these methods are now deprecated and only return the caller’s application process.

Android 5.0 introduced UsageStatsManager which provides access to device usage history and statistics. This API requires the permission android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.


I found this library which may support android devices for versions 6 and below.

GitHub - jaredrummler/AndroidProcesses: DEPRECATED[^]

See if this helps.
Please share if you find any other simplified solutions.
 
Share this answer
 
We get the output of the command and return the String. process.waitFor() is used to wait for the command to finish executing.


try {
Process process = Runtime.getRuntime().exec("/system/bin/ps");
InputStreamReader reader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(reader);
int numRead;
char[] buffer = new char[5000];
StringBuffer commandOutput = new StringBuffer();
while ((numRead = bufferedReader.read(buffer)) > 0) {
commandOutput.append(buffer, 0, numRead);
}
bufferedReader.close();
process.waitFor();

return commandOutput.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900