Some practical PackageUtils gadgets in Android
1. Obtain the meta-data value of the specified key in the application.
public static String getApplicationMetadata(Context context,String metaDataKey) { ApplicationInfo info = null; try { PackageManager pm = context.getPackageManager(); info = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); return String.valueOf(info.metaData.get(metaDataKey)); } catch (Exception e) { e.printStackTrace(); } return null; }
Application Scenario: when the program is packaged and launched to different application markets, you need to set different channel IDs for each apk, and check whether the log information is correct after packaging.
For example:
Call method:PackageUtils.getApplicationMetadata(App.getInstance()
.getContext(), "UMENG_CHANNEL")
The obtained result is Offline.
2. Get the package name
public static String getPackageName(Context context){ return context.getPackageName(); }
3. Get the version name
public static String getVersionName(Context context){ try { PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); String version = info.versionName; return version; } catch (Exception e) { e.printStackTrace(); } return "1.0"; }
4. Get the version number.
public static int getVersionCode(Context context){ try { PackageManager manager = context.getPackageManager(); PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0); int version = info.versionCode; return version; } catch (Exception e) { e.printStackTrace(); } return 1; }
This information is only required to check whether the settings in the program are really not required every time. Therefore, you can print the log information in the activity where the guiding animation of the application is located.