android實現擷取正在啟動並執行應用程式

來源:互聯網
上載者:User

因為在framework中想添加這個功能,所以寫了個appliction來實現一下擷取正在啟動並執行應用程式:
還是先看圖吧:

這個app主要是簡單的實現了擷取非系統的應用程式和一些常用的系統應用程式,顯示在一個listview中,並添加了點擊(回複到你開啟的介面)和長按事件(關閉應用程式)。
看看代碼吧:
直接貼出來再加上注釋吧(直接寫在一個檔案裡): 複製代碼 代碼如下:package andorid.tasks;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;
public class ManagerTasksActivity extends Activity {
private ListView listView;
private PackageManager pm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_main);
pm= this.getPackageManager();//獲得包管理器
listView=(ListView)findViewById(R.id.list_view);
LoadList(this);//載入listview
}
private void LoadList(Context context)
{
ArrayList<HashMap<String, Object>> list=new ArrayList<HashMap<String, Object>>();
try{
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); //獲得ActivityManager對象
List<RunningAppProcessInfo> runningTasks = am.getRunningAppProcesses(); //獲得所有進行中的程式存放在一個list中
for(int i=0;i<runningTasks.size();i++)
{
PackageInfo pInfo=new PackageInfo(context);//獲得PackageInfo對象
//get application which is not in system and the usually
//如果是非系統應用程式以及一些常用的應用程式就加到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(){
public boolean setViewValue(View view,Object data,String textRepresentation){
if(view instanceof ImageView && data instanceof Drawable){
ImageView iv=(ImageView)view;
iv.setImageDrawable((Drawable)data);
return true;
}
else
return false;
}
});
//為listView添加item的點擊事件
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get the item of the list to a hashmap
HashMap<?, ?> map=(HashMap<?, ?>)parent.getItemAtPosition(position);
//get package name from map
String packageName=(String) map.get("packagename");//從前面的map中獲得包名
//if we onclick the item then start the application
//根據包名開啟應用程式
Intent intent=new Intent();
intent =pm.getLaunchIntentForPackage(packageName);
startActivity(intent);
finish();//開啟應用程式之後登出本應用程式
}
});
//為listview的item添加長按事件
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final HashMap<?, ?> long_map=(HashMap<?, ?>)parent.getItemAtPosition(position);
new AlertDialog.Builder(ManagerTasksActivity.this).setTitle("Are you sure close")
.setPositiveButton("sure", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
String packageName=(String)long_map.get("packagename");
//base packagename to kill appliction
//根據包名殺死應用程式
am.killBackgroundProcesses(packageName);
//refash list
//重新整理listview
LoadList(ManagerTasksActivity.this);
}
}).setNegativeButton("cancle", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
return false;
}
});
}
}
//類PackageInfo主要包括ApplicationInfo getInfo(String name)方法
class PackageInfo {
private List<ApplicationInfo> appList;
public PackageInfo(Context context){
//get all package data
PackageManager pm = context.getApplicationContext().getPackageManager();
appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
}
public ApplicationInfo getInfo(String name){
if(name == null){
return null;
}
for(ApplicationInfo appInfo : appList){
if(name.equals(appInfo.processName)){
return appInfo;
}
}
return null;
}
}

xml檔案:
lsit: 複製代碼 代碼如下:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="40dip"
android:layout_height="40dip"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>

main: 複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></ListView>
</LinearLayout>

在manifest檔案中要加一個許可權: 複製代碼 代碼如下:<uses-permission android:name="android.permission.RESTART_PACKAGES" />

主要是前面的am.killBackgroundProcesses(packageName);方法要這個許可權。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.