android实现获取正在运⾏的应⽤程序
因为在framework中想添加这个功能,所以写了个appliction来实现⼀下获取正在运⾏的应⽤程序:
还是先看图吧:
这个app主要是简单的实现了获取⾮系统的应⽤程序和⼀些常⽤的系统应⽤程序,显⽰在⼀个listview中,并添加了点击(回复到你打开的界⾯)和长按事件(关闭应⽤程序)。
看看代码吧:
直接贴出来再加上注释吧(直接写在⼀个⽂件⾥):
复制代码代码如下:
package andorid.tasks;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
alertdialog使用方法import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlertDialog;
t.Context;
t.DialogInterface;
t.DialogInterface.OnClickListener;
t.Intent;
t.pm.ApplicationInfo;
t.pm.PackageManager;
aphics.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) {
setContentView(R.layout.task_main);
pm= PackageManager();//获得包管理器
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 = (SystemService(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(((i).processName).flags&(i).processName).FLAG_SYSTEM)==0 ||((i).processName).equals("acts")
||((i).processName).equals("ail")
||((i).processName).equals("com.android.settings")
||((i).processName).equals("com.android.music")
||((i).processName).equals("com.android.calendar")
||((i).processName).equals("com.android.calculator2")
||((i).processName).equals("com.android.browser")
||((i).processName).equals("com.android.camera")
||((i).processName).equals("dia")
||((i).processName).equals("com.android.bluetooth")
||((i).processName).equals("s"))
{
String dir = (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.(i).processName).loadIcon(pm));
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("icon", (i).processName).loadIcon(pm));
map.put("name", (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", (i).String());//获得包名给后⾯⽤
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加载识别器
//下⾯这个⽅法主要是⽤来刷新图⽚,因为(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<?, ?>)ItemAtPosition(position);
//get package name from map
String packageName=(String) ("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<?, ?>)ItemAtPosition(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)("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 = ApplicationContext().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="schemas.android/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="schemas.android/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);⽅法要这个权限。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论