在framework中想添加這個功能,所以寫了個appliction來實現一下獲取正在運行的應用程序:
還是先看圖吧: 這個app主要是簡單的實現了獲取非系統的應用程序和一些常用的系統應用程序,顯示在一個listview中,並添加了點擊(回復到你打開的界面)和長按事件(關閉應用程序)。 看看代碼吧: 直接貼出來再加上注釋吧(直接寫在一個文件裡): package andorid.tasks; //如果是非系統應用程序以及一些常用的應用程序就加到list中
if((pInfo.getInfo(runningTasks.get(i).processName).flags&pInfo.getInfo(runningTasks.get(i).processName).FLAG_SYSTEM)==0
||(runningTasks.get(i).processName).equals("com.android.contacts")
||(runningTasks.get(i).processName).equals("com.android.email")
||(runningTasks.get(i).processName).equals("com.android.settings")
||(runningTasks.get(i).processName).equals("com.android.music")
||(runningTasks.get(i).processName).equals("com.android.calendar")
||(runningTasks.get(i).processName).equals("com.android.calculator2")
||(runningTasks.get(i).processName).equals("com.android.browser")
||(runningTasks.get(i).processName).equals("com.android.camera")
||(runningTasks.get(i).processName).equals("com.cooliris.media")
||(runningTasks.get(i).processName).equals("com.android.bluetooth")
||(runningTasks.get(i).processName).equals("com.android.mms"))
{
String dir = pInfo.getInfo(runningTasks.get(i).processName).publicSourceDir;
Float size=Float.valueOf((float) ((new File(dir).length()*1.0)));//獲得應用程序的大小如果size大於一M就用M為單位,否則用KB
//long date = new Date(new File(dir).lastModified()).getTime();
//System.out.println(pInfo.getInfo(runningTasks.get(i).processName).loadIcon(pm));
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("icon", pInfo.getInfo(runningTasks.get(i).processName).loadIcon(pm));
map.put("name", pInfo.getInfo(runningTasks.get(i).processName).loadLabel(pm));
if(size>1024*1024)
map.put("info", size/1024/1024+" MB");
else
map.put("info", size/1024+" KB");
map.put("packagename", runningTasks.get(i).processName.toString());//獲得包名給後面用
list.add(map);
}
}
}catch(Exception ex)
{}
SimpleAdapter listadapter=new SimpleAdapter(this, list, R.layout.task_list, new String[]{"icon","name","info"}, new int []{R.id.icon,R.id.name,R.id.info});
listView.setAdapter(listadapter);//listview加載識別器
//下面這個方法主要是用來刷新圖片,因為pInfo.getInfo(runningTasks.get(i).processName).loadIcon(pm)獲得圖片不能被顯示出
listadapter.setViewBinder(new ViewBinder(){