public static int getVerCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e("msg", e.getMessage());
}
return verCode;
}
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.e("msg", e.getMessage());
}
return verName;
}</pre>另一种方法:
<p></p>
<pre name="code" class="java">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();
}
}
android 获取应用程序包名,图标,入口Activity类
最新推荐文章于 2024-07-24 23:45:00 发布