Android获取apk安装包信息

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
public class TestActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  String archiveFilePath="sdcard/jb51.net.apk";//安装包路径
  PackageManager pm = getPackageManager();
   PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
    if(info != null){
     ApplicationInfo appInfo = info.applicationInfo;
     String appName = pm.getApplicationLabel(appInfo).toString();
     String packageName = appInfo.packageName; //得到安装包名称
     String version=info.versionName; //得到版本信息
     // Toast.makeText(TestActivity.this, , Toast.LENGTH_LONG).show();
     Drawable icon = pm.getApplicationIcon(appInfo);//得到图标信息
     TextView tv = (TextView)findViewById(R.id.tv);
     tv.setText("appName:"+appName+"---packageName:"+packageName);
     //显示图标
     ImageView tu=(ImageView)findViewById(R.id.imageView1);
     tu.setBackgroundDrawable(icon);
    }
   }

  }
 }

}







/** 
* 获取apk包的信息:版本号,名称,图标等 
* @param absPath apk包的绝对路径 
* @param context  
*/  
public void apkInfo(String absPath,Context context) {  

    PackageManager pm = context.getPackageManager();  
    PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath,PackageManager.GET_ACTIVITIES);  
    if (pkgInfo != null) {  
        ApplicationInfo appInfo = pkgInfo.applicationInfo;  
        /* 必须加这两句,不然下面icon获取是default icon而不是应用包的icon */  
        appInfo.sourceDir = absPath;  
        appInfo.publicSourceDir = absPath;  
        String appName = pm.getApplicationLabel(appInfo).toString();// 得到应用名  
        String packageName = appInfo.packageName; // 得到包名  
        String version = pkgInfo.versionName; // 得到版本信息  
        /* icon1和icon2其实是一样的 */  
        Drawable icon1 = pm.getApplicationIcon(appInfo);// 得到图标信息  
        Drawable icon2 = appInfo.loadIcon(pm);  
        String pkgInfoStr = String.format("PackageName:%s, Vesion: %s, AppName: %s", packageName, version, appName);  
        Log.i(TAG, String.format("PkgInfo: %s", pkgInfoStr));  
    }  
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android的Px与Dp转化工具类

下一篇:Android获取来电号码代码