有很多软件都有管理我们手机上的应用软件这个功能,比如QQ手机管家等.其主要的就是先查找应用apk,然后可以打开应用,卸载应用,和打开应用管理.由于查找apk有许多属性首先我们创建一个类来储存我们想要的属性.代码如下
public class BaseInfo {
public String filename = "";// 名称
public String fullName = "";// 文件全名,带后缀
public String path = "";// 路径
public long size;// 大小
public String type = "";// 类型
public boolean isSelect = false;// 是否被选中
public long lastModifyTime;
public String realPath;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean isSelect) {
this.isSelect = isSelect;
}
public void setLastModifyTime(long time) {
this.lastModifyTime = time;
}
public long getLastModifyTime() {
return this.lastModifyTime;
}
}
public class AppInfo extends BaseInfo {
public static final int TYPE_APK = 0;
public static final int TYPE_MUSIC = 1;
public String packageName = "";
public String versionName = "";
public String versionCode = "";
public Drawable icon;
// 新添
public Bitmap appbitmap;
public SoftReference<Bitmap> appSoftBitmap;
/** 对应服务器id */
public String aid;
/** 最新版本id */
public String aid_update;
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
public String getVersionCode() {
return versionCode;
}
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public Bitmap getAppbitmap() {
return appbitmap;
}
public void setAppbitmap(Bitmap appbitmap) {
this.appbitmap = appbitmap;
}
public SoftReference<Bitmap> getAppSoftBitmap() {
return appSoftBitmap;
}
public void setAppSoftBitmap(SoftReference<Bitmap> appSoftBitmap) {
this.appSoftBitmap = appSoftBitmap;
}
public String getAid() {
return aid;
}
public void setAid(String aid) {
this.aid = aid;
}
public String getAid_update() {
return aid_update;
}
public void setAid_update(String aid_update) {
this.aid_update = aid_update;
}
public static int getTypeApk() {
return TYPE_APK;
}
public static int getTypeMusic() {
return TYPE_MUSIC;
}
//用于比较
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AppInfo other = (AppInfo) obj;
if (aid == null) {
if (other.aid != null)
return false;
} else if (!aid.equals(other.aid))
return false;
if (aid_update == null) {
if (other.aid_update != null)
return false;
} else if (!aid_update.equals(other.aid_update))
return false;
if (appSoftBitmap == null) {
if (other.appSoftBitmap != null)
return false;
} else if (!appSoftBitmap.equals(other.appSoftBitmap))
return false;
if (appbitmap == null) {
if (other.appbitmap != null)
return false;
} else if (!appbitmap.equals(other.appbitmap))
return false;
if (icon == null) {
if (other.icon != null)
return false;
} else if (!icon.equals(other.icon))
return false;
if (packageName == null) {
if (other.packageName != null)
return false;
} else if (!packageName.equals(other.packageName))
return false;
if (versionCode == null) {
if (other.versionCode != null)
return false;
} else if (!versionCode.equals(other.versionCode))
return false;
if (versionName == null) {
if (other.versionName != null)
return false;
} else if (!versionName.equals(other.versionName))
return false;
return true;
}
}
这样我们的类就写好了.
下面就是查找和打开软件,卸载软件,打开应用管理功能的工具类代码如下:
public class Utils {
/**
* @Title: uninstall
* @Description: 卸载
* @param @param packageName:卸载软件的包名
* @return void
* @throws
*/
/** 卸载 */
public static void uninstall(Context context, String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
uninstallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
}
/**
* 打开应用
*
* @param context
* @param packageName
*/
public static void runApplication(Context context, String packageName) {
// Uri packageURI = Uri.parse("package:" + packageName);
try {
Intent runApplicationIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName);
runApplicationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(runApplicationIntent);
} catch (Exception e) {
}
}
/**
* 打开管理界面
*
* @param context
* @param packageName
*/
public static void openAPKManage(Context context, String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
uninstallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
}
public static ArrayList<AppInfo> getAppInfo(Context context) {
ArrayList<AppInfo> scanningList = new ArrayList<AppInfo>();
PackageManager pm = context.getPackageManager();
// 获取手机内所有应用
List<PackageInfo> paklist = pm.getInstalledPackages(0);
for (int i = 0; i < paklist.size(); i++) {
PackageInfo pak = (PackageInfo) paklist.get(i);
// 判断是否为非系统预装的应用程序
if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
ApplicationInfo info = pak.applicationInfo;
AppInfo apkInfo = new AppInfo();
apkInfo.setFilename((String) info.loadLabel(pm));
apkInfo.setPath(info.sourceDir);
File file = new File(info.sourceDir);
if (!file.exists()) {
continue;
}
apkInfo.setSize(file.length());
apkInfo.setVersionName(pak.versionName);
apkInfo.setVersionCode(String.valueOf(pak.versionCode));
apkInfo.setIcon(info.loadIcon(pm));
apkInfo.setPackageName(pak.packageName);
apkInfo.setFullName(apkInfo.filename + ".apk");
// MyApplication.allPath.add(apkInfo.filename + ".apk");//
// 添加绝对路径
// Mlog.e("应用", "apkInfo.filename:" + apkInfo.filename);
scanningList.add(apkInfo);
}
}
return scanningList;
}
}
主要功能的代码就是这些,还有很多功能可以自己去扩展,希望能够帮助大家.