从jni直接从c++进程调用android活动,无需java端

bfrts1fy  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(326)

我正在使用eclipse开发一个android应用程序,它有java代码和jnic++代码。
我正在尝试直接从jni启动java活动,而不触及java端。有办法吗?

hmmo2u0o

hmmo2u0o1#

好吧,我终于知道怎么做了!这是工作!唯一需要更改的是在中写入包名和类名 NewStringUTF 例如: NewStringUTF("org.samples.test.MyClass") 注: app 是你的 android_app* ```
JNIEnv env;
JavaVM
lJavaVM = app->activity->vm;
app->activity->vm->AttachCurrentThread(&env, NULL);

jobject lNativeActivity = app->activity->clazz;
jclass intentClass = env->FindClass("android/content/Intent");
jstring actionString =env->NewStringUTF("Package Name. Class Name");
jmethodID newIntent = env->GetMethodID(intentClass, "", "()V");
jobject intent = env->AllocObject(intentClass);
env->CallVoidMethod(intent, newIntent);
jmethodID setAction = env->GetMethodID(intentClass, "setAction","(Ljava/lang/String;)Landroid/content/Intent;");
env->CallObjectMethod(intent, setAction, actionString);
jclass activityClass = env->FindClass("android/app/Activity");
jmethodID startActivity = env->GetMethodID(activityClass,"startActivity", "(Landroid/content/Intent;)V");
jobject intentObject = env->NewObject(intentClass,newIntent);
env->CallVoidMethod(intentObject, setAction,actionString);
env->CallVoidMethod(lNativeActivity, startActivity, intentObject);
app->activity->vm->DetachCurrentThread();

您还需要在清单中添加以下代码:



    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

相关问题