I need to get name of my android application from the native side somethin like that:
android.content.context context=(android.content.context) this;//current activiy
Resources appR =context.getResources();
String packageName=context.getPackageName();
int id=appR.getIdentifier("app_name","string",packageName );
CharSequence txt = appR.getText(id);
my native code like that:
jstring Java_com_AnalyticToolC_AnalyticToolActivity_JNISendData(JNIEnv* env,jobject entryObject,jobject contxt)
{
char *realAppName;
realAppName=(char *)malloc(16 * 1024);
jclass android_content_Context =(*env)->GetObjectClass(env, contxt);
jmethodID midGetPackageName = (*env)->GetMethodID(env, android_content_Context, "getPackageName", "()Ljava/lang/String");
jstring packageName=(*env)->CallObjectMethod(env, contxt, midGetPackageName);
jmethodID midGetResources = (*env)->GetMethodID(env, android_content_Context, "getResources", "()L");
jobject jResource=(*env)->CallObjectMethod(env, context, midGetResources);
jclass resource_Class=(*env)->GetObjectClass(env, jResource);
jmethodID midGetIdentifier = (*env)->GetMethodID(env, resource_Class, "getIdentifier", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String)I");
jstring app_name=(*env)->NewStringUTF(env,"app_name");
jstring TypeName=(*env)->NewStringUTF(env,"string");
int id=(*env)->CallObjectMethod(env, jResource, midGetIdentifier,app_name,TypeName,packageName);
jmethodID midGetAppName = (*env)->GetMethodID(env, resource_Class,"getText","(I)Ljava/lang/String");
jstring appName=(*env)->CallObjectMethod(env, jResource, midGetAppName,id);
realAppName=(*env)->GetStringUTFChars(env, appName, NULL);
}
and i just pass the activity to my native methon from java code.
and i don't have a chance to write this code in java class then call it form my NDK application I'm trying a lot to pass a context object as jobject to my native code but it always crash. dose any one have any idea?
Reflected Java access in C is ugly, ugly, ugly, just like you've demonstrated. Pass the app name as an extra string parameter.
EDIT: OK, you want reflection, reflection you'll get.
Your native method belongs to class AnalyticToolActivity
. As a nonstatic class method, it has a this
pointer on every call. Unlike C++ and Java methods, this pointer is passed explicitly as the second parameter. The method has two mandatory parameters - a JNIEnv *
and a jobject
. The second one corresponds to the this
pointer of the Java object.
So if your AnalyticToolActivity
is a subclass of Activity
- quite likely - the entryObject
parameter is an instance of Activity
, meaning it's an instance of Context
. So get rid of the third parameter (contxt
), and your JNI code can go like this:
jclass android_content_Context =(*env)->GetObjectClass(env, entryObject);
//or use FindClass
jmethodID midGetPackageName = (*env)->GetMethodID(env,
android_content_Context,
"getPackageName",
"()Ljava/lang/String;");
jstring packageName=(*env)->CallObjectMethod(env, entryObject, midGetPackageName);
and so forth.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With