package com.jf.install;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jf.install.util.ViewHandler;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity
{
ListView lv;
ListAdapter adapter;
ArrayList< HashMap< String , Object >> items;
private static final int INSTALL = 1;
private InstallThread installThread;
private InstallHandle installHandle;
private LinearLayout mLoad;
long i , j = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewHandler.creteProgressDialog(this, "Loading");
lv = (ListView) findViewById(R.id.applist);
installThread = new InstallThread();
installHandle = new InstallHandle();
new Thread(installThread).start();
}
private void refresh()
{
/**
* 参数:Context ArrayList(item的集合) item的layout 包含ArrayList中的HashMap的key的数组
* key所对应的值的相应的控件id
*/
adapter = new ListAdapter(this, items, R.layout.piitem, new String[]
{ "icon" , "appName" , "packageName" }, new int[]
{ R.id.icon , R.id.appName , R.id.packageName });
lv.setAdapter(adapter);
lv.setOnItemClickListener(new InstallListener());
}
private void iSearchPackage()
{
// 得到PackageManager对象
i = System.currentTimeMillis();
System.out.println("begion:" + i);
PackageManager pm = getPackageManager();
// 得到系统安装的所有程序包的PackageInfo对象
// List<ApplicationInfo> packs = pm.getInstalledApplications(0);
List< PackageInfo > packs = pm.getInstalledPackages(0);
items = new ArrayList< HashMap< String , Object >>();
for (PackageInfo pi : packs)
{
HashMap< String , Object > map = new HashMap< String , Object >();
// 显示用户安装的应用程序,而不显示系统程序
if (( pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM ) == 0 && ( pi.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP ) == 0)
{
// 这将会显示所有安装的应用程序,包括系统应用程序
map.put("icon", pi.applicationInfo.loadIcon(pm));// 图标
map.put("appName", pi.applicationInfo.loadLabel(pm));// 应用程序名称
map.put("packageName", pi.applicationInfo.packageName);// 应用程序包名
// 循环读取并存到HashMap中,再增加到ArrayList上,一个HashMap就是一项
items.add(map);
}
}
j = System.currentTimeMillis();
System.out.println("运行时间:" + ( j - i ));
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("onDestroy");
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("onPause");
}
@Override
protected void onRestart()
{
// TODO Auto-generated method stub
super.onRestart();
new Thread(installThread).start();
ViewHandler.creteProgressDialog(this, "Loading");
System.out.println("onRestart");
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
System.out.println("onResume");
}
@Override
protected void onStart()
{
// TODO Auto-generated method stub
super.onStart();
System.out.println("onStart");
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
System.out.println("onStop");
}
class InstallHandle extends Handler
{
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
super.handleMessage(msg);
if (msg.what == INSTALL)
{
refresh();
ViewHandler.dissMiss();
}
}
}
class InstallThread extends Thread
{
public void run()
{
iSearchPackage();
installHandle.sendEmptyMessage(INSTALL);
}
}
class InstallListener implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView< ? > arg0 , View arg1 , int arg2 , long arg3)
{
// TODO Auto-generated method stub
System.out.println(arg2);
Uri packageURI = Uri.parse("package:" + items.get(arg2).get("packageName"));
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
}
}
package com.jf.install;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.SimpleAdapter.ViewBinder;
public class ListAdapter extends SimpleAdapter
{
private int[] appTo;
private String[] appFrom;
private ViewBinder appViewBinder;
private List<? extends Map<String, ?>> appData;
private int appResource;
private LayoutInflater appInflater;
private Context context;
public ListAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
appData = data;
appResource = resource;
appFrom = from;
appTo = to;
appInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent)
{
return createViewFromResource(position, convertView, parent, appResource);
}
private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource)
{
View v;
if(convertView == null)
{
v = appInflater.inflate(resource, parent,false);
final int[] to = appTo;
final int count = to.length;
final View[] holder = new View[count];
for(int i = 0; i < count; i++)
{
holder[i] = v.findViewById(to[i]);
}
v.setTag(holder);
}else
{
v = convertView;
}
bindView(position, v);
return v;
}
private void bindView(int position, View view)
{
final Map dataSet = appData.get(position);
if(dataSet == null)
{
return;
}
final ViewBinder binder = appViewBinder;
final View[] holder = (View[])view.getTag();
final String[] from = appFrom;
final int[] to = appTo;
final int count = to.length;
for(int i = 0; i < count; i++)
{
final View v = holder[i];
if(v != null)
{
final Object data = dataSet.get(from[i]);
String text = data == null ? "":data.toString();
if(text == null)
{
text = "";
}
boolean bound = false;
if(binder != null)
{
bound = binder.setViewValue(v, data, text);
}
if(!bound)
{
/**
* 自定义适配器,关在在这里,根据传递过来的控件以及值的数据类型,
* 执行相应的方法,可以根据自己需要自行添加if语句。另外,CheckBox等
* 集成自TextView的控件也会被识别成TextView,这就需要判断值的类型
*/
if(v instanceof TextView)
{
//如果是TextView控件,则调用SimpleAdapter自带的方法,设置文本
this.setViewText((TextView)v, text);
}else if(v instanceof ImageView)
{
//如果是ImageView控件,调用自己写的方法,设置图片
setViewImage((ImageView)v, (Drawable)data);
}else
{
throw new IllegalStateException(v.getClass().getName() + " is not a " +
"view that can be bounds by this SimpleAdapter");
}
}
}
}
}
public void setViewImage(ImageView v, Drawable value)
{
v.setImageDrawable(value);
}
}
下面的2个是自定义的加载框:
package com.jf.install.util;
import com.jf.install.R;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ViewHandler {
static ProgressDialog dlg ;
public static ProgressDialog creteProgressDialog(Context context,
String text) {
dlg= new ProgressDialog(context);
dlg.show();
dlg.setContentView(R.layout.loading);
LinearLayout root = (LinearLayout) dlg
.findViewById(R.id.progressDialog);
root.setGravity(android.view.Gravity.CENTER);
LoadingView mLoadView = new LoadingView(context);
mLoadView.setDrawableResId(R.drawable.icon10);
root.addView(mLoadView);
TextView alert = new TextView(context);
Paint tPaint=alert.getPaint();
tPaint.setFakeBoldText(true);
alert.setText(text);
alert.setTextSize(18);
// alert.setTextColor(R.drawable.red);
root.addView(alert);
return dlg;
}
public static void distoryBitmap(Bitmap mFgBitmap){
if ( null !=mFgBitmap&&!mFgBitmap.isRecycled())
mFgBitmap.recycle();
}
public static void dissMiss(){
if(null!=dlg){
System.out.println("dialog dismiss***********************");
dlg.dismiss();
}
}
}
package com.jf.install.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
public class LoadingView extends View {
private Matrix mFgMatrix;
private Bitmap mFgBitmap;
public LoadingView(Context context) {
super(context);
}
public LoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setDrawableResId(int iconResId) {
mFgMatrix = new Matrix();
mFgBitmap = BitmapFactory.decodeResource(getResources(), iconResId);
myHandler.sendEmptyMessage(0);
onMeasure(mFgBitmap.getWidth(), mFgBitmap.getHeight());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(mFgBitmap.getWidth(), mFgBitmap.getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mFgBitmap, mFgMatrix, null);
}
private Handler myHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
mFgMatrix.postRotate(-10f, mFgBitmap.getWidth() / 2f, mFgBitmap
.getHeight() / 2f);
invalidate();
myHandler.sendEmptyMessageDelayed(msg.what, 20);
};
};
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/progressDialog">
</LinearLayout>
<?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/applist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/icon"
android:layout_width="48dip"
android:layout_height="48dip"
android:padding="4dip" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/appName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="@+id/packageName" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>