Android 获取应用程序包名,图标,入口 Activity 类
最近老是用到关于程序包名,图标问题,现总结如下:
PackageInfo info;
try {
info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
// 当前应用的版本名称
String versionName = info.versionName;
// 当前版本的版本号
int versionCode = info.versionCode;
// 当前版本的包名
String packageNames = info.packageName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
定义一个 Info 类来获取
public class Info {
Context context ;
PackageManager pm ;
public Info(Context context) {
this.context = context;
pm = context.getPackageManager();
}
/*
* 获取程序 图标
*/
public Drawable getAppIcon(String packname){
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadIcon(pm);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
*获取程序的版本号
*/
public String getAppVersion(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, 0);
return packinfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/*
* 获取程序的名字
*/
public String getAppName(String packname){
try {
ApplicationInfo info = pm.getApplicationInfo(packname, 0);
return info.loadLabel(pm).toString();
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 获取程序的权限
*/
public String[] getAppPremission(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_PERMISSIONS);
//获取到所有的权限
return packinfo.requestedPermissions;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/*
* 获取程序的签名
*/
public String getAppSignature(String packname){
try {
PackageInfo packinfo = pm.getPackageInfo(packname, PackageManager.GET_SIGNATURES);
//获取到所有的权限
return packinfo.signatures[0].toCharsString();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}