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;
A2Hosting主机,A2Hosting怎么样?A2Hosting是UK2集团下属公司,成立于2003年的老牌国外主机商,产品包括虚拟主机、VPS和独立服务器等,数据中心提供包括美国、新加坡softlayer和荷兰三个地区机房。A2Hosting在国外是一家非常大非常有名气的终合型主机商,拥有几百万的客户,非常值得信赖,国外主机论坛对它家的虚拟主机评价非常不错,当前,A2Hosting主机庆祝1...
很久没有分享PhotonVPS的消息,最近看到商家VPS主机套餐有一些更新所以分享下。这是一家成立于2008年的国外VPS服务商,Psychz机房旗下的站点,主要提供VPS和独立服务器等,数据中心包括美国洛杉矶、达拉斯、芝加哥、阿什本等。目前,商家针对Cloud VPS提供8折优惠码,优惠后最低2G内存套餐每月4美元起。下面列出几款主机配置信息。CPU:1core内存:2GB硬盘:30GB NVm...
ftlcloud(超云)目前正在搞暑假促销,美国圣何塞数据中心的云服务器低至9元/月,系统盘与数据盘分离,支持Windows和Linux,免费防御CC攻击,自带10Gbps的DDoS防御。FTL-超云服务器的主要特色:稳定、安全、弹性、高性能的云端计算服务,快速部署,并且可根据业务需要扩展计算能力,按需付费,节约成本,提高资源的有效利用率。活动地址:https://www.ftlcloud.com...