卸载已安装的程序(非系统自带的程序)

首先,获取已安装的程序(非系统自带的程序)


一、新建一个描述程序的bean类    ApplicationInfo.java

package com.zyf.getapk;

import android.graphics.drawable.Drawable;

public class ApplicationInfo {
	
	public int position;

	public CharSequence title;

	public CharSequence version;
	
	public String packageName;

	public Drawable icon;
	
	public boolean filtered;

}

二、获得已安装的程序    MainActivity.java

private void loadApplications() {
		PackageManager manager = getPackageManager();

		final List<PackageInfo> apps = manager
				.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); // GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的

		if (apps != null) {
			final int count = apps.size();

			if (mApplications == null) {
				mApplications = new ArrayList<ApplicationInfo>(); // 用来存储获取的应用信息数据
			}
			mApplications.clear();

			for (int i = 0; i < count; i++) {
				PackageInfo info = apps.get(i);
				// 过滤掉系统自带的应用
				if (((info.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0 
						&& (info.applicationInfo.flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0)
						|| info.applicationInfo.packageName
								.equalsIgnoreCase(getPackageName())) {
					// 系统自带的应用,continue结束本次循环
					continue;
				}
				ApplicationInfo application = new ApplicationInfo();

				application.title = info.applicationInfo.loadLabel(manager);
				application.icon = info.applicationInfo.loadIcon(manager);
				application.version = "版本:" + info.versionName;
				application.packageName = info.packageName;
				mApplications.add(application);
			}
		}
	}


三、定义数组适配器

private class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> {

		public ApplicationsAdapter(Context context,
				ArrayList<ApplicationInfo> apps) {
			super(context, 0, apps);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final ApplicationInfo info = mApplications.get(position);
			info.position = position;
			if (convertView == null) {
				final LayoutInflater inflater = getLayoutInflater();
				convertView = inflater.inflate(R.layout.it_system, parent,
						false);
			}

			final ImageView imgIcon = (ImageView) convertView
					.findViewById(R.id.icon);
			imgIcon.setImageDrawable(info.icon);
			final TextView textName = (TextView) convertView
					.findViewById(R.id.name);
			textName.setText(info.title);

			final TextView textVersion = (TextView) convertView
					.findViewById(R.id.version);
			textVersion.setText(info.version);

			return convertView;
		}
	}

四、在oncreate中执行异步线程

new MAsynTask().execute();

五、创建异步线程,在异步线程中执行获取应用程序的方法,绑定适配器

	class MAsynTask extends AsyncTask<String, Void, Integer> {

		@Override
		protected Integer doInBackground(String... params) {

			try {
				// 获取应用程序
				loadApplications();
				return 1;
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}

		// 处理doInBackground返回的结果
		@Override
		protected void onPostExecute(Integer result) {
			if (result == 1) {
				adapter = new ApplicationsAdapter(MainActivity.this,
						mApplications);
				listView.setAdapter(adapter);
				findViewById(R.id.load).setVisibility(View.GONE);
				findViewById(R.id.list).setVisibility(View.VISIBLE);
			} else {
				Toast.makeText(MainActivity.this, "加载列表出错", Toast.LENGTH_SHORT)
						.show();
				findViewById(R.id.load).setVisibility(View.GONE);
			}
		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}


然后,写一个卸载程序的方法

	public static void uninstallApk(Context ctx, String packageName) {
		Uri packageURI = Uri.parse("package:" + packageName);
		Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
		ctx.startActivity(uninstallIntent);
	}

点击卸载按钮,执行卸载方法(写在适配器中)

			final Button btnUnload = (Button) convertView
					.findViewById(R.id.btn_uninstall);
			btnUnload.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					uninstallPackage = info.packageName;
					positon = info.position;
					uninstallApk(getContext(), uninstallPackage);
				}
			});

卸载成功之后,要更新界面,写一个广播接受者

	private class UninstallReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (uninstallPackage != null
					&& ("package:" + uninstallPackage).equals(intent
							.getDataString())) {
				mApplications.remove(positon);
				adapter.notifyDataSetChanged();
			}
		}
	}

在oncreate中注册广播

                 mUninstallReceiver = new UninstallReceiver();	
		 IntentFilter filter = new
		 IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
		 filter.addDataScheme("package");
		 this.registerReceiver(mUninstallReceiver, filter);

当Acitivity销毁的时候,广播取消注册

	 @Override
	 protected void onDestroy() {
	 unregisterReceiver(mUninstallReceiver);
	 super.onDestroy();
	 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值