1、apk信息封装类
import android.graphics.drawable.Drawable;
/**
* apk信息封装类
* @author Administrator
*
*/
public class AppBean {
private int uid;
private String apkPath;//apk安装路径
private Drawable icon; //apk的图标
private String appName;//apk的名字
private long size;//占用的大小 单位byte
private boolean isSd;//是否存在sd卡
private boolean isSystem;//是否是系统apk
private String packName;//apk的包名
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public boolean isSd() {
return isSd;
}
public void setSd(boolean isSd) {
this.isSd = isSd;
}
public boolean isSystem() {
return isSystem;
}
public void setSystem(boolean isSystem) {
this.isSystem = isSystem;
}
public String getPackName() {
return packName;
}
public void setPackName(String packName) {
this.packName = packName;
}
public String getApkPath() {
return apkPath;
}
public void setApkPath(String apkPath) {
this.apkPath = apkPath;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
}
2、获取所有安装的apk详细信息核心代码
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
/**
* 获取所有安装的apk详细信息
* @author Administrator
*
*/
public class AppManagerEngine {
/**
* @return
* 返回sd的可用空间,单位 byte
*/
public static long getSDAvail(Context context){
long sdAvail = 0;
//获取sd卡的目录
File externalStorageDirectory = Environment.getExternalStorageDirectory();
sdAvail = externalStorageDirectory.getFreeSpace();
return sdAvail;
}
/**
* @return
* 返回手机剩余空间
*/
public static long getRomAvail(){
long romAvail = 0;
File dataDirectory = Environment.getDataDirectory();
romAvail = dataDirectory.getFreeSpace();
return romAvail;
}
/**
* @return
* 所有安装的apk信息
*/
public static List getAllApks(Context context){
//获取所有安装的apk数据
List apks = new ArrayList();
//获取包管理器
PackageManager pm = context.getPackageManager();
//所有安装的apk
List installedPackages = pm.getInstalledPackages(0);
for (PackageInfo packageInfo : installedPackages) {
AppBean bean = new AppBean();
//设置apk的名字
bean.setAppName(packageInfo.applicationInfo.loadLabel(pm) +"");
//设置apk的包名
bean.setPackName(packageInfo.packageName);
//设置apk的图标
bean.setIcon(packageInfo.applicationInfo.loadIcon(pm));
String sourceDir = packageInfo.applicationInfo.sourceDir;
File file = new File(sourceDir);
//apk的大小
bean.setSize(file.length());
//获取当前apk的flag属性
int flag = packageInfo.applicationInfo.flags;
//是否是系统apk
if ((flag & ApplicationInfo.FLAG_SYSTEM) != 0) {
//系统apk
bean.setSystem(true);//系统apk的标记
} else {
bean.setSystem(false);//用户apk
}
//安装的位置
if ((flag & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
//安装在sd卡中的apk
bean.setSd(true);//中sd中的标记
} else {
bean.setSd(false);//rom中的标记apk
}
//添加apk的路径
bean.setApkPath(packageInfo.applicationInfo.sourceDir);
//添加app的uid
bean.setUid(packageInfo.applicationInfo.uid);
//添加一个apk信息
apks.add(bean);
}
return apks;
}
}
3、另外,可以使用系统提供的api把byte自动转成KB、MB、GB等单位
Formatter.formatFileSize(getApplicationContext(),long类型字节大小);
4、启动应用
// 通过包名获取意图
Intent launchIntentForPackage = pm.getLaunchIntentForPackage(应用包名);
startActivity(launchIntentForPackage);
5、跳转到应用对应的设置界面(卸载&清除数据)
Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.parse("package:" + 应用包名));
startActivity(intent);
6、卸载用户应用
Intent intent = new Intent("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + 应用包名));
startActivity(intent);// 删除用户apk的Activity
7、卸载系统应用
系统apk 默认删除不掉,root刷机,赋予root权限,才可以删除
(1)把RootTools.jar复制到libs目录下,用于判断root权限是否开启和执行shell命令。
(2)删除系统应用代码如下:
//命令写在代码中
try {
//判断手机是否有root
if (!RootTools.isRootAvailable()) {
Toast.makeText(getApplicationContext(), "请先root刷机", 0).show();
return;
}
//是否root权限授权给当前apk
if (!RootTools.isAccessGiven()) {
Toast.makeText(getApplicationContext(), "请先root刷机", 0).show();
return;
}
//直接可以使用命令删除apk
RootTools.sendShell("mount -o remount rw /system", 8000);//设置命令的超时时间为8秒
System.out.println("安装路径:" + clickBean.getApkPath());
RootTools.sendShell("rm -r " + clickBean.getApkPath(), 8000);
RootTools.sendShell("mount -o remount r /system", 8000);
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RootToolsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}