Android 软件管理工具类Utils

时间:2021-09-25 06:55:50
                      Android 软件管理工具类Utils
 /**
* Created by uilubo on 2015/9/30.
* 工具类
*/
public class Utils { public static List<AppInfo> getAppList(Context context){
// 返回值集合
List<AppInfo> list = new ArrayList<AppInfo>(); // 实例化包管理器
PackageManager pm = context.getPackageManager(); // 获取所有已经安装的应用信息
List<PackageInfo> pList = pm.getInstalledPackages(); // 遍历集合
for(int i = ; i<pList.size(); i++){
// 拿到元素
PackageInfo packageInfo = pList.get(i);
//判断是否是第三方应用 同时 不包含当前的应用的包名
if(isThirdPartyApp(packageInfo.applicationInfo) &&
!packageInfo.packageName.equals(context.getPackageName())) {
// 填充实体类
AppInfo app = new AppInfo();
// 获取应用名
app.packageName = packageInfo.packageName;
app.versionName = packageInfo.versionName;
app.versionCode = packageInfo.versionCode;
app.insTime = packageInfo.firstInstallTime;
app.updTime = packageInfo.lastUpdateTime;
app.appName = (String) packageInfo.applicationInfo.loadLabel(pm);
// 获取图标
app.icon = packageInfo.applicationInfo.loadIcon(pm);
// 计算程序的大小
String dir = packageInfo.applicationInfo.publicSourceDir;
long byteSize = new File(dir).length();
// 实际大小
app.byteSize = byteSize;
// 格式化好的大小
app.size = getSize(byteSize);
list.add(app);
}
}
return list;
} /**
* 判断应用是否是第三方应用
* @param appInfo
* @return
*/
public static boolean isThirdPartyApp(ApplicationInfo appInfo) {
boolean flag = false;
if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != ) {
// 可更新的系统应用
flag = true;
}
else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == ) {
// 非系统应用
flag = true;
}
return flag;
} /*获取文件大小*/
public static String getSize(long size){
return new DecimalFormat("0.##").format(size * 1.0/(*));
} /*获取时间*/
public static String getTime(long millis){
Date date = new Date(millis);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
} /*
*打开包文件
*/
public static void openPackage(Context context, String packageName){
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} /*卸载APK安装包*/
public static void uninstallApk(Activity context, String packageName, int requestCode){
Uri uri= Uri.parse("package:"+packageName);
Intent intent = new Intent(Intent.ACTION_DELETE,uri);
context.startActivityForResult(intent,requestCode); } public static List<AppInfo> getSearchResult(List<AppInfo> list,String key){
List<AppInfo> result = new ArrayList<AppInfo>();
for (int i= ; i < list.size(); i++){
AppInfo app = list.get(i);
if (app.appName.toLowerCase().contains(key.toLowerCase())){
result.add(app);
}
}
return result;
} public static SpannableStringBuilder highLightText(String str, String key){
int start = str.toLowerCase().indexOf(key.toLowerCase());// 不区分大小写
int end = start + key.length(); SpannableStringBuilder sb = new SpannableStringBuilder(str);
sb.setSpan(
new ForegroundColorSpan(Color.RED), // 前景颜色
start, // 起始坐标
end, // 终止坐标
Spannable.SPAN_EXCLUSIVE_INCLUSIVE
); return sb;
} }