Issue
My android application compirises two Activities: ".MainActivity" and "android.app.NativeActivity". The latter is implemented purely in C++. On button click in ".MainActivity" I start a native one trying to pass some parameters:
public void pressedButton(View view)
{
Intent intent = new Intent(this, android.app.NativeActivity.class);
intent.putExtra("MY_PARAM_1", 123);
intent.putExtra("MY_PARAM_2", 321);
startActivity(intent);
}
How do I get MY_PARAM_1 and MY_PARAM_2 from within android.app.NativeActivity's entry point (that is a C-function void android_main(struct android_app* state)
)?
Solution
In the android_app
structure there's a data member called activity
of type ANativeActivity*
. Inside the latter, there's a JavaVM *vm
and a misleadingly called jobject clazz
. The clazz
is actually a JNI-compliant object instance pointer to a Java object of type android.app.NativeActivity
, which has all Activity
methods, including getIntent()
.
There's a JNIEnv
there, too, but it looks like it's not attached to the activity's main thread.
Use JNI invokations to retrieve the intent, then extras from the intent. It goes like this:
JNIEnv *env;
state->activity->vm->AttachCurrentThread(&env, 0);
jobject me = state->activity->clazz;
jclass acl = env->GetObjectClass(me); //class pointer of NativeActivity
jmethodID giid = env->GetMethodID(acl, "getIntent", "()Landroid/content/Intent;");
jobject intent = env->CallObjectMethod(me, giid); //Got our intent
jclass icl = env->GetObjectClass(intent); //class pointer of Intent
jmethodID gseid = env->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
jstring jsParam1 = (jstring)env->CallObjectMethod(intent, gseid, env->NewStringUTF("MY_PARAM_1"));
const char *Param1 = env->GetStringUTFChars(jsParam1, 0);
//When done with it, or when you've made a copy
env->ReleaseStringUTFChars(jsParam1, Param1);
//Same for Param2
Answered By - Seva Alekseyev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.