[Work accumulation] NDK gets package name and version from Java

Source: Internet
Author: User

////////////////////////////////////////////////////////////////////////////Java Code Snippet//get APK ' s Versioncode in Androidmanifest.xml Public intGetversioncode () {intVersioncode = 1;Try{packageinfo PackageInfo= This. Getpackagemanager (). Getpackageinfo (Getpackagename (), 0); Versioncode=Packageinfo.versioncode;}Catch(namenotfoundexception e) {log.d (TAG,"Cannot get package info.");}returnVersioncode;}

//  --------------------------------------------------------------------
//C code snippet:helper function to get the package name from Java// --------------------------------------------------------------------Static BOOLAndroid_getpackagename (Char*outpackagename, size_t length) {Android_app* App =Android_getapp (); Anativeactivity*activity = app->activity; JNIEnv* env = activity->env; //note:we need to attach Dalvik VM-to-current thread, as it's not main threadjavavm* VM = activity->VMS; if((*VM)->getenv (VM, (void* *) &env, jni_version_1_6) <0 )        (*VM)->attachcurrentthread (VM, &env, NULL); //Get package name from Activity Class (context)Jclass Android_content_context = (*env)->getobjectclass (env, activity->clazz); Jmethodid Midgetpackagename= (*env)->getmethodid (env, Android_content_context,"Getpackagename","() ljava/lang/string;"); Jstring PackageName= (jstring) (*env)->callobjectmethod (env, activity->Clazz, Midgetpackagename); BOOLRET =false; if(PackageName! =NULL ) {        //Jchar is UTF16Jsize len = (*env)getstringlength (env, PackageName); Constjchar* Jcharbuff = (*env)Getstringchars (env, PackageName, NULL); //Copy Jcharbuff to outpckagename with code conversion (UTF16 to UTF8)        #errorDo code Conversion        (*ENV)deletelocalref (env, PackageName); }    (*ENV)deletelocalref (env, android_content_context); returnret;}// --------------------------------------------------------------------//helper function to get the package version code from Java// --------------------------------------------------------------------Static intAndroid_getversioncode () {Android_app* App =Android_getapp (); Anativeactivity*activity = app->activity; //get version code from Javajnienv* env = activity->env; //note:we need to attach Dalvik VM-to-current thread, as it's not main threadjavavm* VM = activity->VMS; if((*VM)->getenv (VM, (void* *) &env, jni_version_1_6) <0 )        (*VM)->attachcurrentthread (VM, &env, NULL); Jclass Activityclass= (*env)->getobjectclass (env, activity->clazz); Jmethodid Java_method= (*env)->getmethodid (env, Activityclass, (char8*)"Getversioncode", (char8*)"() I"); ASSERT (Java_method!=NULL); JINT ret= (*env)->callintmethod (env, activity->Clazz, Java_method); (*ENV)deletelocalref (env, activityclass); return(int) ret;}

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.