概述
android应用/游戏上架国内渠道,国外GooglePlay接入的登录或者支付验证需要验证包名,versionCode,签名等应用信息,为了快速检查应用信息以工具化获取应用信息。
tips:在Activity下直接用getPackageManager可获取PackageManager实例
1、应用名:getPackageManager().getApplicationInfo(packName,0).loadLabel(getPackageManager());
返回CharSequence
2、应用Icon:getPackageManager().getApplicationInfo(packName,0).loadIcon(getPackageManager());
返回drawable
3、VersionCode:getPackageManager().getPackageInfo(packName,0).versionCode
4、VersionName:getPackageManager().getPackageInfo(packName,0).versionName
5、targetSdkVersion:getPackageManager().getPackageInfo(packName,0).applicationInfo.targetSdkVersion
6、minSDKVersion:getPackageManager().getPackageInfo(packName,0).applicationInfo.minSdkVersion
7、启动项:getPackageManager().getLaunchIntentForPackage(packName)
返回Intent,可用于跳转
获取启动项类名:getPackageManager().getLaunchIntentForPackage(packName).getComponent().getClassName()
8、权限:getPackageManager().getPackageInfo(packName,PackageManager.GET_PERMISSIONS).requestedPermissions
返回类型为字符串数组
9、activity组件:getPackageManager().getPackageInfo(packName, PackageManager.GET_ACTIVITIES).activities
返回ActivityInfo数据类
PackageInfo packageInfo = getPackageManager().getPackageInfo(packName, PackageManager.GET_ACTIVITIES)
for (ActivityInfo activityInfo : packageInfo.activities){
//获取activity名称
Log.d(TAG,activityInfo.name+"n");
}
10、service组件:getPackageManager().getPackageInfo(packName, PackageManager.GET_SERVICES).services
返回ServiceInfo数据类
PackageInfo packageInfo = getPackageManager().getPackageInfo(packName, PackageManager.GET_SERVICES)
for (ServiceInfo serviceInfo : packageInfo.services){
//获取service名称
Log.d(TAG,serviceInfo.name+"n");
}
11、provider组件:getPackageManager().getPackageInfo(packName, PackageManager.GET_PROVIDERS).providers
返回ProviderInfo数据类
PackageInfo packageInfo = getPackageManager().getPackageInfo(packName, PackageManager.GET_PROVIDERS)
for (ProviderInfo providerInfo : packageInfo.providers){
//获取provider名称
Log.d(TAG,activityInfo.name+"n");
}
12、获取keyhash:
/**获取Keyhash*/
public static String getKeyhash(Activity activity, String packName){
String KeyHash = "";
try {
int i = 0;
PackageInfo info = activity.getPackageManager().getPackageInfo( packName, PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
i++;
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
KeyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
//KeyHash
Log.e(tag, "KeyHash: " + KeyHash);
}
}
catch (Exception e) {
Log.e(tag,"获取keyhash失败"+e.getMessage());
KeyHash = "";
}
return KeyHash;
}
13、获取签名MD5,SHA1,SHA256:
/**获取SHA1
* @param activity 当前activity
* @param packName 包名
* @param type 获取数据类型:MD5,SHA1,SHA256
* */
public static String getSignatures(Activity activity, String packName, String type){
String sha1 = "";
PackageInfo info = null;
try {
info = activity.getPackageManager().getPackageInfo(packName, PackageManager.GET_SIGNATURES);
byte[] cert = info.signatures[0].toByteArray();
//type:MD5,SHA1,SHA256
MessageDigest md = MessageDigest.getInstance(type);
byte[] publicKey = md.digest(cert);
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < publicKey.length; i++) {
String appendString = Integer.toHexString(0xFF & publicKey[i])
.toUpperCase(Locale.US);
if (appendString.length() == 1){
hexString.append("0");
}
hexString.append(appendString);
hexString.append(":");
}
String result = hexString.toString();
Log.d(tag,"SHA1:"+result.substring(0, result.length()-1));
return result.substring(0, result.length()-1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
14、获取META_DATA标签指定key的value
/**获取节点value*/
public static String getMeta(Activity activity, String packName, String key){
ApplicationInfo applicationInfo = null;
String value = "";
try {
applicationInfo = activity.getPackageManager().getApplicationInfo(packName,PackageManager.GET_META_DATA);
value = String.valueOf(applicationInfo.metaData.get(key));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}catch (NullPointerException e){
e.printStackTrace();
}
return value;
}
最后
以上就是俊逸奇迹为你收集整理的Android通过包名获取应用信息的全部内容,希望文章能够帮你解决Android通过包名获取应用信息所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复