call android activity from jni directly from c++ process without java side(直接从没有java端的c ++进程从jni调用android活动)
问题描述
我正在使用 Eclipse 并开发一个具有 Java 编码和 jni C++ 代码的 Android 应用程序.
I'm using Eclipse and developing an Android app which have a Java coding and also jni C++ code.
我正在尝试直接从 jni 启动 Java 活动,而不涉及 Java 端.有办法吗?
I'm trying to start an activity of Java directly from the jni without touching the Java side. Is there a way to do so?
推荐答案
好的,我终于知道怎么做了!这是工作!您唯一需要更改的是将您的包名和类名写入NewStringUTF
例如:NewStringUTF("org.samples.test.MyClass")
ok I found out how to do it finally! it's work!
The only thing you need to change is write your package name and class name in
NewStringUTF
for example: 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, "<init>", "()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();
您还需要在清单中添加此代码:
you also need to add this code in your manifest:
<activity android:name=".JavaActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="you.package.path.JavaActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
这篇关于直接从没有java端的c ++进程从jni调用android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:直接从没有java端的c ++进程从jni调用android活动
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01