卸载已安装的程序(非系统自带的程序)
来源:互联网 发布:h5动画源码 编辑:程序博客网 时间:2024/05/16 10:53
首先,获取已安装的程序(非系统自带的程序)
一、新建一个描述程序的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);}@Overridepublic 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> {@Overrideprotected Integer doInBackground(String... params) {try {// 获取应用程序loadApplications();return 1;} catch (Exception e) {e.printStackTrace();return 0;}}@Overrideprotected void onCancelled() {super.onCancelled();}// 处理doInBackground返回的结果@Overrideprotected 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);}}@Overrideprotected void onPreExecute() {}@Overrideprotected 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() {@Overridepublic void onClick(View v) {uninstallPackage = info.packageName;positon = info.position;uninstallApk(getContext(), uninstallPackage);}});
卸载成功之后,要更新界面,写一个广播接受者
private class UninstallReceiver extends BroadcastReceiver {@Overridepublic 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
- 卸载已安装的程序(非系统自带的程序)
- 如何在自己的App中调用Android系统自带的安装/卸载程序
- 巧妙卸载windows自带的程序
- SAP系统自带的示例程序
- SAP系统自带的示例程序
- android系统自带的程序调用
- android 如何调用系统自带的程序安装器安装程序
- android调用自带的程序安装
- 完全卸载 ubuntu 中已安装程序的 命令
- Android模拟器中卸载系统自带程序(转)
- 卸载linux系统自带的mysql
- CentOS卸载系统自带的OpenJDK
- LINUX 卸载系统自带 JDK 安装新的 jdk(1.6)
- windows自带的程序
- Android之获取手机上已安装的所有的应用程序的信息,并判断是否安装了某应用程序(系统程序或非系统应用程序判断)
- 卸载已经安装的非myeclipse自带tomcat--非myeclipse自带tomcat安装--myeclipse中关联新安装的tomcat--
- 可以删除的android系统自带程序
- 可以删除的android系统自带程序2
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- 理解C++复杂声明
- c#试验九4678合集
- 第十周项目4-大奖赛计分4
- 利用jquery给指定的table动态添加一行、删除一行
- 卸载已安装的程序(非系统自带的程序)
- C++字符串
- C#特殊字符处理
- OpenCV2.3使用摄像头和视频
- 吃法(一)
- 上海脑瘫治疗哪家好
- debian一键安装saltstack
- android Bimtap 各种图片处理方法、图片特效
- Swift新手入门汇集帖