在没有java的情况下,直接从jni调用android活动。[英] call android activity from jni directly from c++ process without java side

本文是小编为大家收集整理的关于在没有java的情况下,直接从jni调用android活动。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用Eclipse并开发具有Java编码以及JNI C ++代码的Android应用程序.

我正在尝试直接从JNI开始JAVA的活动,而无需触摸Java侧. 有办法这样做吗?

推荐答案

好的,我发现了如何做到这一点!这是工作! 您唯一需要更改的是在中写下包装名称和班级名称 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, "<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();

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

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

本文地址:https://www.itbaoku.cn/post/102533.html

问题描述

I'm using Eclipse and developing an Android app which have a Java coding and also jni C++ code.

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?

推荐答案

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

note: app is your 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>