Reputation: 487
I want to develop a task manager like application where I want to display the list of runnung services and the memory used by each service in a list view.
I figured that running services can be obtained using
ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> procInfos = am.getRunningServices(maxNum);
But how to dislay them in a ListView and also how to obtain the memory used by each one of them? I am an android beginner and I wish anyone could explain me in a detailed manner...
I tried the following. I could see the service name etc in logcat but the apllication is force closing.
package com.example.ServiceList;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ServiceListActivity extends Activity {
/** Called when the activity is first created. */
int maxNum = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
this.setContentView(tv);
android.os.Debug.MemoryInfo[] mInfo;
int[] processid = null;
ActivityManager am = (ActivityManager)this.getSystemService (Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> procInfos = am.getRunningServices(maxNum);
for(int i = 0; i < procInfos.size(); i++){
tv.setText(tv.getText().toString()+ " " + "\n");
Log.v("TaskMgr Stringe", procInfos.get(i).service.toString());
Log.v("TaskMgr class name", procInfos.get(i).service.getClassName());
processid[i]=procInfos.get(i).pid;
String s = " "+processid[i];
Log.v("TaskMgr process id",s );
Log.v("TaskMgr process memory", am.getProcessMemoryInfo(processid[0]);
}
mInfo = am.getProcessMemoryInfo(processid);
int dp= mInfo[0].dalvikPss;
String s1= " " + dp;
tv.setText(tv.getText().toString()+ " " + s1 );
}
}
Upvotes: 0
Views: 307
Reputation: 13541
You should begin by looking at what a RunningServiceInfo has that is available... that you can use for your ListView's data.
If this does not have what you need then you may need to look at the source of how the original battery application that is packaged in the source of android does it. A good branch to look at would be version 2.3.3.
Always having the android source to reference will save you a lot a headache.
Upvotes: 1