欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现获取正在运行的应用程序

程序员文章站 2023-12-01 08:30:10
因为在framework中想添加这个功能,所以写了个appliction来实现一下获取正在运行的应用程序: 还是先看图吧: 这个app主要是简单的实现了获取非系统的应用程序...
因为在framework中想添加这个功能,所以写了个appliction来实现一下获取正在运行的应用程序:
还是先看图吧:
android实现获取正在运行的应用程序
这个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);方法要这个权限。