Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android pass parameter to Native Activity

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))?

like image 828
Nick Avatar asked Oct 11 '12 13:10

Nick


People also ask

How do you pass data between activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you pass data from one activity to another activity using intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

How do I transfer data from one Android app to another?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.

Which is the parent class of intent object in Android?

The LabeledIntent is the subclass of android.


1 Answers

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
like image 119
Seva Alekseyev Avatar answered Sep 19 '22 12:09

Seva Alekseyev