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;
819云互联是海外领先的互联网业务平台服务提供商。专注为用户提供低价高性能云计算产品,致力于云计算应用的易用性开发,并引导云计算在国内普及。目前平台研发以及运营云服务基础设施服务平台(IaaS),面向全球客户提供基于云计算的IT解决方案与客户服务,拥有丰富的海外资源、香港,日本,美国等各国优质的IDC资源。官方网站:https://www.819yun.com香港特价物理服务器:地区CPU内存带宽...
博鳌云是一家以海外互联网基础业务为主的高新技术企业,运营全球高品质数据中心业务。自2008年开始为用户提供服务,距今11年,在国人商家中来说非常老牌。致力于为中国用户提供域名注册(国外接口)、免费虚拟主机、香港虚拟主机、VPS云主机和香港、台湾、马来西亚等地服务器租用服务,各类网络应用解決方案等领域的专业网络数据服务。商家支持支付宝、微信、银行转账等付款方式。目前香港有一款特价独立服务器正在促销,...
tmhhost怎么样?tmhhost正在搞暑假大促销活动,全部是高端线路VPS,现在直接季付8折优惠,活动截止时间是8月31日。可选机房及线路有美国洛杉矶cn2 gia+200G高防、洛杉矶三网CN2 GIA、洛杉矶CERA机房CN2 GIA,日本软银(100M带宽)、香港BGP直连200M带宽、香港三网CN2 GIA、韩国双向CN2。点击进入:tmhhost官方网站地址tmhhost优惠码:Tm...