当前位置: 编程技术>移动开发
android实现获取正在运行的应用程序
来源: 互联网 发布时间:2014-10-12
本文导语: 因为在framework中想添加这个功能,所以写了个appliction来实现一下获取正在运行的应用程序: 还是先看图吧: 这个app主要是简单的实现了获取非系统的应用程序和一些常用的系统应用程序,显示在一个listview中,并添加了点击...
因为在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 list=new ArrayList();
try{
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); //获得ActivityManager对象
List runningTasks = am.getRunningAppProcesses(); //获得所有正在进行的程序存放在一个list中
for(int i=0;i1024*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 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:
main:
在manifest文件中要加一个权限:
主要是前面的am.killBackgroundProcesses(packageName);方法要这个权限。
还是先看图吧:
这个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 list=new ArrayList();
try{
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); //获得ActivityManager对象
List runningTasks = am.getRunningAppProcesses(); //获得所有正在进行的程序存放在一个list中
for(int i=0;i1024*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 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:
代码如下:
main:
代码如下:
在manifest文件中要加一个权限:
代码如下:
主要是前面的am.killBackgroundProcesses(packageName);方法要这个权限。