Android工具类:快速删除指定应用程序

懒骨头(http://blog.csdn.com/iamlazybone)

开发时使用的工具类,不通用,发文权当笔记。

记得很多群里问怎么得到已安装的应用程序列表,下面的例子用到了。

/** * 一个不通用的工具类:一键删除某个指定应用程序。开发时的临时需求,不通用权当笔记。 * * @author lazybone */ public class HiYoGameBox extends Activity { public List<ResolveInfo> myApps;// 应用程序列表 public String filterStr = "com.ipmsg";// 过滤字符串 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载要操作的应用程序列表 loadAppsList(); // 如果未找到程序直接退出 if (myApps.size() == 0) { finish(); return; } // 删除应用程序 deleteApps(myApps); } /** * 删除应用程序 */ public void deleteApps(List<ResolveInfo> myApps) { String packageName = myApps.get(0).activityInfo.packageName; Uri uri = Uri.fromParts("package", packageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivityForResult(it, 0); Log.i("", "Delete apps ok"); } /** * 操作完成后关闭此程序 */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { this.finish(); } /** * 加载应用列表 */ private void loadAppsList() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); myApps = getPackageManager().queryIntentActivities(mainIntent, 0); // 过滤一下 for (int i = myApps.size() - 1; i >= 0; i--) { String packageName = myApps.get(i).activityInfo.packageName; if (!packageName.contains(filterStr)) { myApps.remove(i); } } } /** * 开始游戏 * * @param name * @param packageName */ public void startGame(String name, String packageName) { if (name == null || name.equals("")) { return; } Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName cn = new ComponentName(packageName, name); intent.setComponent(cn); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } /** * 适配器类,九宫格显示应用程序列表 * * @author lazybone */ public class AppsAdapter extends BaseAdapter { public View getView(int position, View convertView, ViewGroup parent) { ImageView image = new ImageView(HiYoGameBox.this); ResolveInfo info = myApps.get(position % myApps.size()); image.setImageDrawable(info.activityInfo .loadIcon(getPackageManager())); image.setScaleType(ImageView.ScaleType.FIT_CENTER); image.setLayoutParams(new LinearLayout.LayoutParams(48, 48)); TextView text = new TextView(HiYoGameBox.this); text.setText("" + info.activityInfo.loadLabel(getPackageManager())); text.setLayoutParams(new LinearLayout.LayoutParams(60, LayoutParams.FILL_PARENT)); text.setTextColor(Color.BLACK); text.setMaxLines(2); LinearLayout layout = new LinearLayout(HiYoGameBox.this); layout.setLayoutParams(new GridView.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); layout.setOrientation(LinearLayout.VERTICAL); layout.addView(image); layout.addView(text); return layout; } public final int getCount() { return Math.min(48, myApps.size()); } public final Object getItem(int position) { return myApps.get(position % myApps.size()); } public final long getItemId(int position) { return position; } } 

你可能感兴趣的:(android,String,layout,null,delete,工具)