项目结构如图,MyToast类是自定义的Toast类,MainActivity是主要Activity类
MainActivity.java:
package com.taskManager.activity;
import java.util.ArrayList; import java.util.List;
import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends Activity implements Runnable , OnItemClickListener{ //Handler用来识别的标志 private static final int SEARCH_APP = 0; private static final int DELETE_APP = 1; GridView gv; ListView lv; //加载程序信息的List private List<PackageInfo> packageInfo; //存放手机所有程序信息的List private List<PackageInfo> showPackageInfo; //存放用户安装程序信息的List private List<PackageInfo> userPackageInfo; private ProgressDialog pd; ImageButton ib_change_view; ImageButton ib_change_category ; private boolean allApplication = true; private boolean isListView = false; /** * 程序一个Handler,用来接收进程传过来的信息 * 然后设置ListView和GridView的适配器 */ private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //接收成功 if(msg.what == SEARCH_APP) { showPackageInfo = packageInfo; lv.setAdapter(new ListViewAdapter(MainActivity.this,showPackageInfo)); gv.setAdapter(new GridViewAdapter(MainActivity.this,showPackageInfo)); pd.dismiss(); } else if(msg.what == DELETE_APP){ } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去除标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //全屏显示 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); findView(); initSettings(); } /** * 加载视图 */ private void findView() { gv = (GridView)this.findViewById(R.id.gv_apps); lv = (ListView)this.findViewById(R.id.lv_apps); pd = ProgressDialog.show(this,"温馨提示","加载中,请稍等...",true,false); ib_change_category = (ImageButton)this.findViewById(R.id.ib_change_category); ib_change_view = (ImageButton)this.findViewById(R.id.ib_change_view); }
/** * 初始化设置 */ private void initSettings() { Thread t = new Thread(this); t.start(); ib_change_category.setOnClickListener(changeAppsCategory); ib_change_view.setOnClickListener(changeListView); gv.setOnItemClickListener(this); lv.setOnItemClickListener(this); } //添加GridView ItemClick监听器 private GridView.OnItemClickListener gvItemClickListener = new GridView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }; //ListView ItemClick监听器 private ListView.OnItemClickListener lvItemClickListener = new ListView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { } }; //点击事件 private ImageButton.OnClickListener changeListView = new ImageButton.OnClickListener(){ @Override public void onClick(View v) { if(isListView){ MyToast.myToastShow(MainActivity.this, R.drawable.grids, "表格视图", Toast.LENGTH_SHORT); ib_change_view.setImageResource(R.drawable.grids); lv.setVisibility(View.GONE); gv.setVisibility(View.VISIBLE); isListView = false; } else { MyToast.myToastShow(MainActivity.this, R.drawable.list, "列表视图", Toast.LENGTH_SHORT); ib_change_view.setImageResource(R.drawable.list); lv.setVisibility(View.VISIBLE); gv.setVisibility(View.GONE); isListView = true; } } }; //点击事件 private ImageButton.OnClickListener changeAppsCategory = new ImageButton.OnClickListener(){ @Override public void onClick(View v) { if(allApplication){ ib_change_category.setImageResource(R.drawable.user); showPackageInfo = userPackageInfo; allApplication = false; MyToast.myToastShow(MainActivity.this, R.drawable.user, "用户的APP", Toast.LENGTH_SHORT); } else{ MyToast.myToastShow(MainActivity.this, R.drawable.all, "所有的APP", Toast.LENGTH_SHORT); ib_change_category.setImageResource(R.drawable.all); showPackageInfo = packageInfo; allApplication = true; } gv.setAdapter(new GridViewAdapter(MainActivity.this,showPackageInfo)); lv.setAdapter(new ListViewAdapter(MainActivity.this,showPackageInfo)); } };
//GridView的适配器类 class GridViewAdapter extends BaseAdapter{
LayoutInflater inflater; List<PackageInfo> pkInfo; public GridViewAdapter(Context context,List<PackageInfo> packageInfos){ inflater = LayoutInflater.from(context); this.pkInfo = packageInfos; } @Override public int getCount() { return pkInfo.size(); }
@Override public Object getItem(int arg0) { return pkInfo.get(arg0); }
@Override public long getItemId(int position) { return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.gv_item, null); TextView tv = (TextView)view.findViewById(R.id.gv_item_appname); ImageView iv =(ImageView) view.findViewById(R.id.gv_item_icon); tv.setText(pkInfo.get(position).applicationInfo.loadLabel(getPackageManager())); iv.setImageDrawable(pkInfo.get(position).applicationInfo.loadIcon(getPackageManager())); return view; } }
//ListView的适配器类 class ListViewAdapter extends BaseAdapter{ LayoutInflater inflater; List<PackageInfo> pkInfo; public ListViewAdapter(Context context,List<PackageInfo> packageInfos){ inflater = LayoutInflater.from(context); this.pkInfo = packageInfos; } @Override public int getCount() { return pkInfo.size(); } @Override public Object getItem(int arg0) { return pkInfo.get(arg0); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = inflater.inflate(R.layout.list_item, null); TextView tv_appName = (TextView)view.findViewById(R.id.lv_item_appname); TextView tv_packageName = (TextView)view.findViewById(R.id.lv_item_packageame); ImageView iv =(ImageView) view.findViewById(R.id.lv_icon); tv_appName.setText(pkInfo.get(position).applicationInfo.loadLabel(getPackageManager())); tv_packageName.setText(pkInfo.get(position).packageName); iv.setImageDrawable(pkInfo.get(position).applicationInfo.loadIcon(getPackageManager())); return view; } } /** * 开一个进程,用Handler来传送Message */ @Override public void run() { packageInfo = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); userPackageInfo = new ArrayList<PackageInfo>(); for (int i =0;i<packageInfo.size();i ){ PackageInfo temp = packageInfo.get(i); ApplicationInfo appInfo = temp.applicationInfo; boolean flag = false; //用户安装的程序 if((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){ flag = true; } else if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) ==0){ flag = true; } if(flag){ userPackageInfo.add(temp); } } //进程睡眠3秒 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } mHandler.sendEmptyMessage(SEARCH_APP); try { Thread.currentThread(); Thread.sleep(5000); //用Handler传送Message mHandler.sendEmptyMessage(DELETE_APP); } catch (InterruptedException e) { e.printStackTrace(); } } /** * ItemClick的处理 */ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { final PackageInfo tempPkInfo = showPackageInfo.get(position); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选项"); builder.setItems(R.array.choice, new DialogInterface.OnClickListener(){
@Override public void onClick(DialogInterface dialog, int which) { switch(which){ case 0: showAppDetail(tempPkInfo); break; case 1: ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); activityManager.killBackgroundProcesses(tempPkInfo.packageName); MyToast.myToastShow(getApplicationContext(), R.drawable.manage, "成功关闭进程!", 1000); break; } } }); builder.setNegativeButton("取消", null); builder.create(); builder.show(); } /** * Activity回传处理 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); packageInfo = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); userPackageInfo = new ArrayList<PackageInfo>(); for (int i =0;i<packageInfo.size();i ){ PackageInfo temp = packageInfo.get(i); ApplicationInfo appInfo = temp.applicationInfo; boolean flag = false; //用户安装的程序 if((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){ flag = true; } else if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) ==0){ flag = true; } if(flag){ userPackageInfo.add(temp); } } if(allApplication){ showPackageInfo = packageInfo; } else{ showPackageInfo = userPackageInfo; } gv.setAdapter(new GridViewAdapter(MainActivity.this,showPackageInfo)); lv.setAdapter(new ListViewAdapter(MainActivity.this,showPackageInfo)); }
/** *显示程序的详细详细 */ private void showAppDetail(PackageInfo packageInfo) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("程序详细详细"); StringBuffer messageDetail = new StringBuffer(); messageDetail.append("程序名称: " packageInfo.applicationInfo.loadLabel(getPackageManager())); messageDetail.append("n包名: " packageInfo.packageName); messageDetail.append("n版本: " packageInfo.versionName); builder.setMessage(messageDetail); builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager())); builder.setPositiveButton("确定", null); builder.create().show(); } /** * 回退按钮处理 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){ AlertDialog.Builder alertbBuilder=new AlertDialog.Builder(MainActivity.this); alertbBuilder.setTitle("提示").setMessage("确认退出?").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }).create(); //显示提示框 alertbBuilder.show(); } return super.onKeyDown(keyCode, event); } }
MyToast.java:
package com.taskManager.activity;
import android.content.Context; import android.graphics.Color; import android.view.Gravity; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; /** * *自定义的Toast类 */ public class MyToast { public static void myToastShow(Context context,int imageResId,String content, int duration){ Toast toast = new Toast (context); toast.setDuration(duration); toast.setGravity(Gravity.BOTTOM, 0, 25); LinearLayout toastLayout = new LinearLayout(context); toastLayout.setOrientation(LinearLayout.HORIZONTAL); toastLayout.setGravity(Gravity.CENTER_VERTICAL); //设置Toast的图标 ImageView imageView = new ImageView(context); imageView.setImageResource(imageResId); toastLayout.addView(imageView); //设置Toast的Text TextView tv_content = new TextView(context); tv_content.setText(content); tv_content.setBackgroundColor(Color.BLACK); toastLayout.addView(tv_content); toast.setView(toastLayout); toast.show(); } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background = "#313849" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_height = "28px" android:layout_width = "fill_parent" android:orientation ="horizontal" android:gravity = "center_vertical" android:paddingLeft = "5px" android:background ="@drawable/top_bg" > <ImageView android:layout_width = "18px" android:layout_height ="18px" android:src = "@drawable/manage"/> <TextView android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:textColor = "#000" android:textSize= "14px" android:paddingLeft = "10px" android:paddingTop = "5px" android:text = "任务管理器 v1.0.0"/> </LinearLayout> <GridView android:id="@ id/gv_apps" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:numColumns = "3" android:horizontalSpacing = "10px" android:verticalSpacing = "10px" android:listSelector = "@drawable/choose_gridview" android:layout_marginLeft = "5px" android:layout_marginTop = "28px" android:layout_marginBottom = "58px" android:layout_marginRight = "5px"/> <ListView android:id="@ id/lv_apps" android:layout_width="fill_parent" android:layout_height="fill_parent" android:listSelector="@drawable/choose_listview" android:layout_marginTop="28px" android:layout_marginBottom="58px" android:layout_marginLeft="5px" android:layout_marginRight="5px" android:visibility="gone" > </ListView> <RelativeLayout android:layout_width = "fill_parent" android:layout_height ="58px" android:layout_alignParentBottom = "true" android:background = "@drawable/bottom_bg" > <ImageButton android:id="@ id/ib_change_view" android:layout_alignParentLeft="true" android:layout_marginLeft="5px" android:layout_marginTop="1px" android:src="@drawable/list" android:layout_width="76px" android:layout_height="54px"/> <ImageButton android:id="@ id/ib_change_category" android:layout_alignParentRight="true" android:layout_marginRight="5px" android:layout_marginTop="1px" android:src="@drawable/all" android:layout_width="76px" android:layout_height="54px"/> </RelativeLayout>
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- 整体是一个LinearLayout, 1、 布局方向为水平布局, 2、 宽fill_parent 3、 高wrap_content 4、 内容的方向垂直居中 5、 ImageView a) Id为lv_icon b) 距离父控件的顶部和底部都是5px c) 宽、高48px 6、 LinearLayout a) 布局方向为垂直布局 b) 宽wrap_content c) 高48px d) 左边预留5px的距离 e) TextView i. Id为lv_item_appname ii. 宽fill_parent iii. 高wrap_content iv. 单行显示 v. 字体大小16px vi. 字体样式:加粗 vii. 字体颜色:#fff f) TextView i. Id为lv_item_packagenam ii. 宽fill_parent iii. 高wrap_content iv. 单行显示 v. 字体颜色#fff -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:id="@ id/lv_icon" android:layout_width="48px" android:layout_height="48px" android:layout_marginTop="5px" android:layout_marginBottom="5px" ></ImageView> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="48px" android:paddingLeft="5px" > <TextView android:id="@ id/lv_item_appname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:textSize="16px" android:textStyle="bold" android:textColor="#fff" ></TextView> <TextView android:id="@ id/lv_item_packageame" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:textColor="#fff" ></TextView> </LinearLayout> </LinearLayout>
gv_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
a) LinearLayout i. 高:wrap_content ii. 宽:90px iii. 布局方向:垂直 iv. 设置里面的控件的位置为中间 v. ImageView 1. 设置id为gv_item_icon 2. 宽高都为64px vi. TextView 1. 设置id为gv_item_appname 2. 宽高都为wrap_content 3. 设置为2行 4. 字体大小16px 5. 颜色#FFF --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="90px" android:layout_height="wrap_content" android:gravity="center">
<ImageView android:id="@ id/gv_item_icon" android:layout_width="64px" android:layout_height="64px"> </ImageView> <TextView android:id="@ id/gv_item_appname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lines="2" android:textSize="16px" android:textColor="#FFF" /> </LinearLayout>
string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AppExplorer!</string> <string name="app_name">安卓任务管理器</string> <array name="choice"> <item name="detail">详细信息</item> <item name="close">关闭进程</item> </array> </resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.taskManager.activity" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
图片: