程序管理器(非原创-转载)


  
 

 
  
 

 
 
 

 

public class MainActivity extends Activity implements Runnable,
		OnItemClickListener, OnClickListener {
	private static final int SEARCH_APP = 0;
	private static final int DELETE_APP = 1;
	GridView gridView;
	ListView listView;
	private List<PackageInfo> allpkgInfo;
	private List<PackageInfo> userpkgInfo;//用户程序
	private List<PackageInfo> showpkgInfo;//要显示的程序
	private ProgressDialog pd;
	ImageButton btn2;
	ImageButton btn1;
	private boolean allApplication = true;
	private boolean isListView = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);

		setProgressBarIndeterminateVisibility(true);

		init();
		LayoutAnimationController controller = new LayoutAnimationController(
				setAnimationMethod(), 1);
		gridView.setLayoutAnimation(controller);// 设置动画效果
		listView.setLayoutAnimation(controller);// 设置动画效果
		listView.setCacheColorHint(0);

		pd = ProgressDialog.show(this, "请稍候...", "正在搜索应用程序...", true, false);
		Thread t = new Thread(this);
		t.start();

	}

	private void init() {
		gridView = (GridView) this.findViewById(R.id.gv_apps);
		listView = (ListView) this.findViewById(R.id.lv_apps);
		gridView.setOnItemClickListener(this);
		listView.setOnItemClickListener(this);
		btn2 = (ImageButton) this.findViewById(R.id.ib_change_category);
		btn1 = (ImageButton) this.findViewById(R.id.ib_change_view);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_change_category:// 显示系统还是显示只安装的
			if (allApplication) {//只显示用户安装的
				btn2.setImageResource(R.drawable.user);
				showpkgInfo = userpkgInfo;
				allApplication = false;
				MyToast.myToastShow(MainActivity.this, R.drawable.user,"用户安装的程序列表", 2000);
			} else {//显示所有的
				btn2.setImageResource(R.drawable.all);
				showpkgInfo = allpkgInfo;
				allApplication = true;
				MyToast.myToastShow(MainActivity.this, R.drawable.all,"所有程序列表", 2000);
			}
			gridView.setAdapter(new GridViewAdapter(MainActivity.this,showpkgInfo));
			listView.setAdapter(new ListViewAdapter(MainActivity.this,showpkgInfo));
			break;
		case R.id.ib_change_view:// 显示方式:列表还是网格视图
			if (isListView) {// 网格显示
				MyToast.myToastShow(MainActivity.this, R.drawable.grids,"网格显示", 2000);
				btn1.setImageResource(R.drawable.grids);
				listView.setVisibility(View.GONE);
				gridView.setVisibility(View.VISIBLE);
				isListView = false;
			} else {
				MyToast.myToastShow(MainActivity.this, R.drawable.list,"列表显示", 2000);
				btn1.setImageResource(R.drawable.list);
				gridView.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				isListView = true;
			}
			break;
		default:
			break;
		}
	}


	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == SEARCH_APP) {
				showpkgInfo = allpkgInfo;
				gridView.setAdapter(new GridViewAdapter(MainActivity.this,showpkgInfo));
				listView.setAdapter(new ListViewAdapter(MainActivity.this,showpkgInfo));
				pd.dismiss();
				setProgressBarIndeterminateVisibility(false);
			}
			if (msg.what == DELETE_APP) {
				System.out.println("Delete App Success!!");
				Toast.makeText(MainActivity.this, "卸载成功!", 2000).show();
			}
		}
	};

	// GridView的适配器
	class GridViewAdapter extends BaseAdapter {

		LayoutInflater inflater;
		List<PackageInfo> pkInfos;

		public GridViewAdapter(Context context, List<PackageInfo> pkgInfo) {
			inflater = LayoutInflater.from(context);
			this.pkInfos = pkgInfo;
		}

		public int getCount() {
			return pkInfos.size();
		}

		public Object getItem(int arg0) {
			return pkInfos.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		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(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
			iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
			return view;
		}

	}

	// ListView的适配器
	class ListViewAdapter extends BaseAdapter {
		LayoutInflater inflater;
		List<PackageInfo> pkInfos;

		public ListViewAdapter(Context context, List<PackageInfo> pkgInfo) {
			inflater = LayoutInflater.from(context);
			this.pkInfos = pkgInfo;
		}

		public int getCount() {
			return pkInfos.size();
		}

		public Object getItem(int arg0) {
			return pkInfos.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = inflater.inflate(R.layout.lv_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(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
			tv_packagename.setText(pkInfos.get(position).packageName);
			iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
			return view;
		}
	}

	@SuppressWarnings("static-access")
	@Override
	public void run() {
		allpkgInfo = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES| PackageManager.GET_ACTIVITIES);
		userpkgInfo = new ArrayList<PackageInfo>();
		
		for (int i = 0; i < allpkgInfo.size(); i++) {
			PackageInfo temp = allpkgInfo.get(i);
			ApplicationInfo appInfo = temp.applicationInfo;
			boolean flag = false;
			if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
				// Updated system app
				flag = true;
			} else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
				// Non-system app
				flag = true;
			}
			if (flag) {
				userpkgInfo.add(temp);
			}
		}

		try {
			Thread.currentThread().sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		mHandler.sendEmptyMessage(SEARCH_APP);
		try {
			Thread.currentThread().sleep(5000);
			mHandler.sendEmptyMessage(DELETE_APP);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
		
		final PackageInfo tempPkInfo = showpkgInfo.get(position);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("选项");
		builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0://启动程序
					String packageName = tempPkInfo.packageName;
					ActivityInfo activityInfo = tempPkInfo.activities[0];
					if (activityInfo == null) {
						Toast.makeText(MainActivity.this, "没有任何activity",2000).show();
						return;
					}
					String activityName = activityInfo.name;
					Intent intent = new Intent();
					intent.setComponent(new ComponentName(packageName,activityName));
					startActivity(intent);
					break;
				case 1://详细信息
					showAppDetail(tempPkInfo);
					break;
				case 2://卸载程序
					Uri packageUri = Uri.parse("package:"+ tempPkInfo.packageName);
					Intent deleteIntent = new Intent();
					deleteIntent.setAction(Intent.ACTION_DELETE);
					deleteIntent.setData(packageUri);
					startActivityForResult(deleteIntent, 0);
					break;
				}

			}
		});
		builder.setNegativeButton("取消", null);
		builder.create().show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		allpkgInfo = getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES| PackageManager.GET_ACTIVITIES);
		
		userpkgInfo = new ArrayList<PackageInfo>();
		for (int i = 0; i < allpkgInfo.size(); i++) {
			PackageInfo temp = allpkgInfo.get(i);
			ApplicationInfo appInfo = temp.applicationInfo;
			boolean flag = false;
			if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
				// Updated system app
				flag = true;
			} else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
				// Non-system app
				flag = true;
			}
			if (flag) {
				userpkgInfo.add(temp);
			}
		}

		if (allApplication) {
			showpkgInfo = allpkgInfo;
		} else {
			showpkgInfo = userpkgInfo;
		}
		gridView.setAdapter(new GridViewAdapter(MainActivity.this,showpkgInfo));
		listView.setAdapter(new ListViewAdapter(MainActivity.this,showpkgInfo));
	}

	// 详细信息
	private void showAppDetail(PackageInfo packageInfo) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("详细信息");
		StringBuffer message = new StringBuffer();
		message.append("程序名称:"+ packageInfo.applicationInfo.loadLabel(getPackageManager()));
		message.append("\n 包名:" + packageInfo.packageName);
		message.append("\n 版本号:" + packageInfo.versionCode);
		message.append("\n 版本名:" + packageInfo.versionName);
		builder.setMessage(message.toString());
		builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));
		builder.setPositiveButton("确定", null);
		builder.create().show();
	}

	// AlphaAnimation 控制渐变透明的动画效果
	// ScaleAnimation 控制尺寸伸缩的动画效果
	// TranslateAnimation 控制画面平移的动画效果
	// RotateAnimation 控制画面角度变化的动画效果
	// LayoutAnimation 渲染ViewGroup中每个View显示时候的动画效果
	private AnimationSet setAnimationMethod() {
		// 设置动画效果
		AnimationSet set = new AnimationSet(false);
		Animation animation = new AlphaAnimation(0, 1);
		animation.setDuration(500);
		set.addAnimation(animation);

		animation = new TranslateAnimation(1, 13, 10, 50);
		animation.setDuration(300);
		set.addAnimation(animation);

		animation = new RotateAnimation(30, 10);
		animation.setDuration(300);
		set.addAnimation(animation);

		animation = new ScaleAnimation(5, 0, 2, 0);
		animation.setDuration(300);
		set.addAnimation(animation);
		return set;
	}

}

 
 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	android:background="#313849">
	<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"></ImageView>
		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:textColor="#000"
			android:textSize="14px" android:text="应用程序"></TextView>
	</LinearLayout>

	<GridView android:id="@+id/gv_apps" android:layout_height="fill_parent"
		android:layout_width="fill_parent" android:numColumns="3"
		android:horizontalSpacing="10px" android:verticalSpacing="10px"
		android:listSelector="@drawable/choose_gridview"
		android:layout_marginTop="28px" android:layout_marginBottom="58px"
		android:layout_marginLeft="5px" android:layout_marginRight="5px"
		android:layoutAnimation="@anim/layout_anim">
	</GridView>

	<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" android:layoutAnimation="@anim/layout_anim">
	</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>
		<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"></ImageButton>
	</RelativeLayout>
</RelativeLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值