Created
April 19, 2014 14:43
-
-
Save douzifly/11086408 to your computer and use it in GitHub Desktop.
android get package name from jni
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static jstring getpkg(JNIEnv* env, jobject thiz, jobject activity) { | |
jclass android_content_Context =env->GetObjectClass(activity); | |
jmethodID midGetPackageName = env->GetMethodID(android_content_Context,"getPackageName", "()Ljava/lang/String;"); | |
jstring packageName= (jstring)env->CallObjectMethod(activity, midGetPackageName); | |
return packageName; | |
} |
for reference, a way to do it without activity parameter:
JNIEXPORT jstring JNICALL
Java_com_example_test_Test_getPackageName(JNIEnv *env, jobject obj)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetMethodID(env, cls, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject packageManager = (*env)->CallObjectMethod(env, obj, mid);
// this.getPackageName()
mid = (*env)->GetMethodID(env, cls, "getPackageName", "()Ljava/lang/String;");
jstring packageName = (jstring) (*env)->CallObjectMethod(env, obj, mid);
return packageName;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good code. But do you know a way of performing this without
activity
handle?