AndroidManifest property parsing and code dynamic acquisition of attribute values

Source: Internet
Author: User

Parse some attributes of the AndroidManifest. xml file and obtain the specified configuration value in the code.

           
 


1) manifest --> package
The package name of an application, which is the unique identifier of each application. Each application must specify this value. The system uses this value to differentiate and find different applications.
Code for obtaining this attribute:

/*** Get the application package name (apk package name, not the registration of the class) * @ param conext Activity in the program, application Instances can be */public static String getAppPackageName (Context context) {if (context! = Null) {return context. getPackageName ();} return "";}



2) manifest --> versionCode
The version number of the current app, which is used for app upgrade. An apk with a small versionCode can be upgraded to an apk with a large versionCode. However, an apk with a small versionCode cannot overwrite a large version of versionCode and must be uninstalled first.

Code for obtaining this attribute:

/*** Get the program version * @ param conext Activity in the program. The Application instance can * @ return-1 get failed */public static int getAppVersionCode (Context context Context) {try {PackageManager pm = context. getPackageManager (); PackageInfo pi = pm. getPackageInfo (context. getPackageName (), 0); return pi. versionCode;} catch (Exception e) {e. printStackTrace (); GoogleAnalyticsTool. getInstance (). sendException (1, e);} return-1 ;}



3) manifest --> versionName
Current app version name
Code for obtaining this attribute:

/*** Get the program version name * @ param conext Activity in the program. The Application instance can all be */public static String getAppVersionName (Context context) {String versionName = ""; try {PackageManager pm = context. getPackageManager (); PackageInfo pi = pm. getPackageInfo (context. getPackageName (), 0); versionName = pi. versionName;} catch (Exception e) {e. printStackTrace (); GoogleAnalyticsTool. getInstance (). sendException (1, e);} return versionName ;}




4) manifest --> application --> debuggable
Whether to enable the debug mode. This attribute can be left unspecified.
True: it is in debug mode. You can debug the application using a tool and break the breakpoint.

False: Non-debug mode. You cannot debug an application using a debugging tool. The breakpoint is invalid.

Code for obtaining this attribute:

/*** Get whether it is the Activity in the debug mode * @ param conext program. The Application instance can all be */public static boolean isDebugMode (Context context) {if (context = null) {Log. e ("Utils: isDebugMode", "context is null"); return false;} ApplicationInfo = context. getApplicationInfo (); return (0! = (Info. flags) & ApplicationInfo. FLAG_DEBUGGABLE ));}


For more information, see jiese1990.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.