Android开发中apk安装包常用工具
开发项目中用到的有关获取apk信息的工具类在做版本升级的时候一般都需要用到部分方法做点笔记public class AppUtil {
*根据应用包名package name启动应用
*参数分别是上下文c ontext和应用包名 pkgNamepublic static boolean startApkByPkgName(Context context,String pkgName) {
P ac kageManager pac kageManager=c ontext.getP ac kageManager();
Intent intent=new Intent();intent=pac kageManager.getLaunc hIntentF orP ac kage(pkgName);if(intent==null) {
System.out.println(APP not found!r eturn fals e;c ontext.startAc tivity(intent);return true;
*根据apk文件路径得到app信息public static ApplicationInfo getApkInfoByPath(Context context,String absPath) {
ApplicationInfo appInfo=null;
Pac kageManager pm=c ontext.getPac kageManager();
PackageInfo pkgInfo =pm.getPac kageArchiveInfo(absPath,Pac kageManager.GET_ACTI VITIES);if(pkgInfo!=null) {appInfo=pkgInfo.applic ationInfo;
/*必须加这两句不然下面icon获取是default icon而不是应用包的icon*/
appInfo.s ourc eDir=absPath;appInfo.public Sourc eDir=absPath;return appInfo;
*根据apk应用包名得到应用信息public static PackageInfo getApkInfoByPkgName(Context context,String pkgName) {PackageInfo pkgInfo=null;
Pac kageManager pm=c ontext.getPac kageManager();try{
//0代表是获取版本信息pkgInfo=pm.getPac kageInfo(pkgName,0);
} c atch(NameNotFoundExc eption e) {
//TODO Auto-generated catch blocke.printStackTrac e();return pkgInfo;
*根据apk文件路径得到应用信息public static PackageInfo getPkgInfoByPath(Context c ontext,String absPath) {PackageInfo pkgInfo=null;
Pac kageManager pm=c ontext.getPac kageManager();pkgInfo=pm.getPackageArchiveInfo(absPath,
Pac kageManager.GET_ACTI VITIES);return pkgInfo;
*根据apk路径直接跳到安装界面
public static void installApkByPath(Context context,String absPath) {
Intent intent=new Intent(Intent.ACTION_VIEW);intent.s etDataAndType(Uri.pars e(file://+abs Path),applic ation/vnd.android.package-archiveintent.s etFlags(Intent.FLAG_ACTIVITY_NEW_TAS K);c ontext.startAc tivity(intent);
*根据apk包名卸载应用public static void uninstallApk(Context context,String packageName) {
String uriString=package:+pac kageName;
Uri uninstallUrl=Uri.parse(uriString);
Intent intent=new Intent(Intent.ACTION_DELET E,uninstallUrl);c ontext.startAc tivity(intent);public static List ResolveInfo queryAppInfo(Context c ontext,
String packageName) {
Pac kageManager pm=c ontext.getPac kageManager(); //获得Pac kageManager对象Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);mainIntent.addCategory(Intent.CAT EGO RY_LAUNC HER);mainIntent.s etP ac kage(pac kageName);
//通过查询获得所有ResolveInfo对象.
List ResolveInfo resolveInfos=pm.queryIntentActivities(mainIntent,
P ac kageManager.GET_INTENT_FILTERS);return resolveInfos;
*根据名名获取版本信息public static int getInstalledAppVersionCode(Context context,String pkgName) {int versionCode=-1;
PackageInfo pkgInfo=AppUtil.getPkgInfoByName(c ontext,pkgName);if(pkgInfo!=null) {versionCode=pkgInfo.versionCode;return versionCode;
Pia云这个商家的云服务器在前面也有介绍过几次,从价格上确实比较便宜。我们可以看到最低云服务器低至月付20元,服务器均采用KVM虚拟架构技术,数据中心包括美国洛杉矶、中国香港、俄罗斯和深圳地区,这次春节活动商家的活动力度比较大推出出全场6.66折,如果我们有需要可以体验。初次体验的记得月付方案,如果合适再续约。pia云春节活动优惠券:piayun-2022 Pia云服务商官方网站我们一起看看这次活...
官方网站:点击访问CDN客服QQ:123008公司名:贵州青辞赋文化传媒有限公司域名和IP被墙封了怎么办?用cloudsecre.com网站被攻击了怎么办?用cloudsecre.com问:黑客为什么要找网站来攻击?答:黑客需要找肉鸡。问:什么是肉鸡?答:被控的服务器和电脑主机就是肉鸡。问:肉鸡有什么作用?答:肉鸡的作用非常多,可以用来干违法的事情,通常的行为有:VPN拨号,流量P2P,攻击傀儡,...
官方网站:点击访问青云互联活动官网优惠码:终身88折扣优惠码:WN789-2021香港测试IP:154.196.254美国测试IP:243.164.1活动方案:用户购买任意全区域云服务器月付以上享受免费更换IP服务;限美国区域云服务器凡是购买均可以提交工单定制天机防火墙高防御保护端口以及保护模式;香港区域购买季度、半年付、年付周期均可免费申请额外1IP;使用优惠码购买后续费周期终身同活动价,价格不...