由于手机内存有限,有时需要一次删除很多个包,这时就需要一个实现Android包批量删除的应用。

首先是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="5" > <TextView
android:id="@+id/textView1"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4" /> <TextView
android:id="@+id/textView2"
style="@style/my_style"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="应用名称" /> <TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
android:gravity="center_vertical|center_horizontal"
android:text="是否系统应用" /> </LinearLayout> <ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1" > </ListView> <Button
android:id="@+id/btn_apk"
style="@style/my_style"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:text="卸载" /> </LinearLayout>

布局文件相对简单,最重要的是列表。

然后是类文件:

package com.hzhi.sysinfor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class apk<PackageDeleteObserver> extends ListActivity{ Button btn_unload;
PackageManager pkg_man;
PackageDeleteObserver pkg_obs;
list_apk list_ada; // 获得所有的应用列表
public void get_app(){ list_ada = new list_apk(this); // 包管理器
PackageManager pm = getPackageManager();
// 获取手机内所有应用
List<PackageInfo> pi = pm.getInstalledPackages(0); for (int i=0; i<pi.size(); i++){ PackageInfo pii = (PackageInfo) pi.get(i);
String is_sys;
Drawable icon; // 是否系统应用
if ((pii.applicationInfo.flags & pii.applicationInfo.FLAG_SYSTEM) <= 0)
is_sys = "否";
else
is_sys = "是"; if (pii.applicationInfo.loadIcon(pm) != null)
icon = (Drawable)pii.applicationInfo.loadIcon(pm);
else
icon = (Drawable) getResources().getDrawable(R.drawable.ic_launcher); list_ada.addItem(String.valueOf(pii.applicationInfo.loadLabel(pm)),
is_sys,
icon,
pii.applicationInfo.packageName,
false); } setListAdapter(list_ada);
} @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_apk); get_app(); btn_unload = (Button) findViewById(R.id.btn_apk);
btn_unload.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { for (int j=0; j<list_ada.list_data.size(); j++){ // 该应用需要删除
if (list_ada.list_data.get(j).is_chk){ String name = (String)
list_ada.list_data.get(j).
                                            txt_name.getText();
final String pak = list_ada.list_data.get(j).item_pak; new AlertDialog.Builder(apk.this)
.setTitle("删除应用")
.setMessage("确定删除" + name + "吗?")
.setPositiveButton("是",
                                  new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface arg0, int arg1)
                                     {
unload(pak);
get_app();
} })
.setNegativeButton("否", null)
.show();
} } }
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public boolean unload (String n){ boolean res = true; try{
// 通过包名创建URI
Uri pu = Uri.parse("package:" + n);
// 创建Intent
Intent i = new Intent(Intent.ACTION_DELETE, pu);
// 执行卸载程序
startActivity(i);
res = true;
}
catch(Exception e){
res = false;
}
finally{
return res;
} } } //apk列表
class list_apk extends BaseAdapter implements OnClickListener{ public Context ctx;
public List<item_apk> list_data; public list_apk(Context context){ ctx = context;
list_data = new ArrayList<item_apk>(); } @Override
public int getCount() {
// TODO Auto-generated method stub
return list_data.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list_data.get(arg0);
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return list_data.indexOf(arg0);
} @Override
public View getView(int position, View convertView, ViewGroup parent) { item_apk my_item;
Integer i = 0; if (convertView == null)
{
my_item = new item_apk(ctx,
(String)list_data.get(position).txt_name.getText(),
(String)list_data.get(position).txt_flag.getText(),
list_data.get(position).img_apk.getDrawable(),
list_data.get(position).item_pak,
list_data.get(position).is_chk);
}
else
{
my_item = (item_apk)convertView;
my_item.txt_name.setText(list_data.get(position).txt_name.getText());
my_item.txt_flag.setText(list_data.get(position).txt_flag.getText());
my_item.img_apk.setImageDrawable(list_data.get(position).img_apk.getDrawable());
}
CheckBox chk_item = (CheckBox) my_item.chk_apk;
chk_item.setOnClickListener(this);
if (list_data.get(position).is_chk)
Log.i(String.valueOf(position) + ".is_chk=", "true");
else
Log.i(String.valueOf(position) + ".is_chk=", "false");
chk_item.setChecked(list_data.get(position).is_chk);
chk_item.setTag(position); return my_item; } public void addItem(String txt_name, String txt_flag, Drawable ico_apk,
String str_name, Boolean bol_chk)
{
list_data.add(new item_apk(ctx,txt_name,txt_flag,ico_apk,str_name,bol_chk));
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox c = (CheckBox) v;
int pos = Integer.parseInt(v.getTag().toString());
list_data.get(pos).is_chk = c.isChecked();
} } //apk列表的一行
class item_apk extends LinearLayout{ public CheckBox chk_apk;
public TextView txt_name;
public TextView txt_flag;
public ImageView img_apk;
public String item_pak;
public boolean is_chk; public item_apk(Context ctx, String item_name, String item_flag,
Drawable item_draw, String str_name, Boolean bol_chk)
{
super(ctx);
this.setOrientation(HORIZONTAL); int hei = 60; chk_apk = new CheckBox(ctx);
addView(chk_apk,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),hei)); img_apk = new ImageView(ctx);
img_apk.setImageDrawable(item_draw);
addView(img_apk,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),hei)); txt_name = new TextView(ctx);
txt_name.setText(item_name);
addView(txt_name,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.4),hei)); txt_flag = new TextView(ctx);
txt_flag.setText(item_flag);
addView(txt_flag,
new LinearLayout.LayoutParams((int)(MainActivity.wid_scr*0.2),hei)); item_pak = str_name;
is_chk = bol_chk; } }

一共实现了三个类:apk、list_apk、item_apk。apk是整个Activity的类,list_apk是列表的适配器类,item_apk是列表中一行的类。list_apk的list_data是重要的变量,它记录了每一个包的信息,如包名、是否是系统应用、是否被选中等。

点击“卸载”按钮时,遍历list_data的所有元素,判断是否被选中,如果被选中,就删除。

运行效果如下:

可见,选中的两个包已经被删除。

实现Android包的批量删除的更多相关文章

  1. Android 长按Listview显示CheckBox,实现批量删除。

    ListView实现的列表,如果是可编辑,可删除的,一般都要提供批量删除功能,否则的话,一项一项的删除体验很不好,也给用户带来了很大的麻烦. 实现效果图 具体实现代码 select.xml 主布局文件 ...

  2. Android ListView的批量处理(多选/反选/删除)

    在Android开发中经常遇到使用ListView的情况,有时候需要的不仅仅是列表显示,还有长按列表进行多选,并且批量删除的情况,在这里记录一下自己的所学. 先上效果图: 几个需要用到的核心方法: / ...

  3. 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)

    1.导入jar watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/400 ...

  4. 基于注解Spring MVC综合Hibernate(需要jar包,spring和Hibernate整合配置,springMVC组态,重定向,)批量删除

    1.进口jar 2.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app ver ...

  5. StackExchange.Redis加载Lua脚本进行模糊查询的批量删除和修改

    前言 使用StackExchange.Redis没有直接相关的方法进行模糊查询的批量删除和修改操作,虽然可以通过Scan相关的方法进行模糊查询,例如:HashScan("hashkey&qu ...

  6. mybatis(二)接口编程 、动态sql 、批量删除 、动态更新、连表查询

    原理等不在赘述,这里主要通过代码展现. 在mybatis(一)基础上,新建一个dao包,并在里面编写接口,然后再在xml文件中引入接口路径,其他不变,在运用阶段将比原始方法更节约时间,因为不用再去手动 ...

  7. Android JUnit Test——批量运行测试代码

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ Android测试三要素 写Android测试用例有三要素,一是我们用的“安卓模拟器device” ...

  8. Zencart批量删除无图片产品

    Zencart批量删除无图片产品 2012-04-23 07:26:18|  分类: 默认分类 |字号 订阅 转自 http://zhongjia33.blog.163.com/blog/#m=0   ...

  9. 18-09-11 软件rpm yum rm卸载 和批量删除

    一 在Linux下删除文件用rm命令,具体用法如下: rm [选项] 文件 选项说明: -f -force 忽略不存在的文件,强制删除,无任何提示 -i --interactive 进行交互式地删除 ...

随机推荐

  1. SQL面试题

    Student(S#,Sname,Sage,Ssex) 学生表     S#:学号:Sname:学生姓名:Sage:学生年龄:Ssex:学生性别Course(C#,Cname,T#) 课程表     ...

  2. 打包上传成功, itunes connect 不出现上传的版本

    由于有一次感觉build setting 里 code signing identity设置证书有点乱,有些已经用不到了,就想把那些删除 于是我找到了所有证书的那个文件夹(进入所有证书的那个文件夹), ...

  3. H5常用代码:适配方案2

    前面的通过视口做适配的方案由于安卓低版本原生浏览器的存在,在许多场合不尽如人意,会在低版本安卓上出现,不缩放,手动缩放未禁止的问题. 于是出现了第二种适配方案,既然通过视口缩放可以兼容,那为什么不直接 ...

  4. Atitit 迭代法  “二分法”和“牛顿迭代法 attilax总结

    Atitit 迭代法  "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...

  5. 玩转Django的POST请求 CSRF

    玩转Django的POST请求 CSRF 不少麻油们玩django都会碰到这个问题,POST请求莫名其妙的返回 403 foribidden,希望这篇博文能解答所有问题 三种方法 To enable ...

  6. Python数据类型之“数字(numerics)”

    上一节内容说的是"Python基本语法",本节主要讲下Python中的数据类型. 存储在内存中的数据通常有两个属性: 在内存中的存放位置:这个存放位置通过变量名可以找到: 在内存中 ...

  7. OpenCascade Modeling Algorithms Fillets and Chamfers

    Modeling Algorithms Fillets and Chamfers 造型算法——倒圆与倒角 eryar@163.com 一.倒圆Fillet Constructor 1. BRepFil ...

  8. Bmob基础

    BaaS是指专为移动应用开发者提供整合云后端的服务.开发者无需过多研究服务器端程序,而只需调用云计算平台提供的API,使用相应SDK,就能迅速完成数据存储.账户管理.消息推送.社交网络整合等功能.本文 ...

  9. 批处理:Windows主机通过FTP下载远程Linux主机上文件

    问题:在Windows上怎么写个批处理把多个文件FTP依次下载到本地某个目录. 批处理脚本示例: @echo off title Download db files. Don't close it!! ...

  10. 初探JavaScript(三)——JS带我"碰壁"带我飞

    已经写了两篇关于小白的JavaScript之行,不可否认,每一种语言都有其精华与糟粕之处,来不及细细体味其精华奥妙,也没法对其评头论足,只能先了解,后深入.到目前为止已经看完<JavaScrip ...