由于手机内存有限,有时需要一次删除很多个包,这时就需要一个实现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的所有元素,判断是否被选中,如果被选中,就删除。
运行效果如下:
可见,选中的两个包已经被删除。