从NDK获取应用程序的名称 [英] get application name from NDK
问题描述
我需要从本机端事端一样,让我的Android应用程序的名称:
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);
我的家乡code这样的:
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);
}
和我只是传递活动,我从Java code本地methon。
and i just pass the activity to my native methon from java code.
和我没有机会用Java编写的类此code然后调用它构成了我NDK的应用
我想了很多上下文对象jobject传递给我的家乡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?
推荐答案
用C反映Java访问是丑陋的,丑陋的,丑陋的,就像你证明。通过应用程序名称作为一个额外的字符串参数。
Reflected Java access in C is ugly, ugly, ugly, just like you've demonstrated. Pass the app name as an extra string parameter.
编辑:好,你要反思,反思你会得到
OK, you want reflection, reflection you'll get.
您的本地方法属于类 AnalyticToolActivity
。作为一个非静态类方法,它有一个在每次调用这个
指针。与C ++和Java的方法,该指针明确传递作为第二个参数。该方法有两个强制性参数 - 一个的JNIEnv *
和 jobject
。第二个对应于这个
的Java对象的指针。
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.
所以,如果你的 AnalyticToolActivity
是活动
的一个子类 - 很可能 - 在 entryObject
参数活动的一个实例
,这意味着它的上下文
的一个实例。因此,摆脱了第三个参数( contxt
),和你的JNI code可以是这样的:
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);
和等等。
这篇关于从NDK获取应用程序的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!