我想制作一个列表视图,我将在其中显示正在运行的进程。查找进程的命令是getSystemService(getActivity().ACTIVITY_SERVICE).getRunningAppProcesses()
,它返回带有进程的arraylist。我的问题是如何在进程关闭时更新列表视图。我设法用一个Thread(下面的代码)来实现它,但我不确定它的效率如何。还有其他想法吗?
线程代码
new Thread(){
@Override
public void run(){
try {
synchronized (this) {
while (true) {
wait(2500);
procs.clear();
ramProcs.clear();
procs.addAll(manager.getRunningAppProcesses());
Iterator itr = procs.iterator();
while (itr.hasNext()) {
ramProcs.add(manager.getProcessMemoryInfo(new int[]{((ActivityManager.RunningAppProcessInfo) (itr.next())).pid})[0].getTotalPss() * 0.001);
}
System.gc();
if (procs.size() == ramProcs.size()) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();