直接从没有java端的c ++进程从jni调用android活动

call android activity from jni directly from c++ process without java side(直接从没有java端的c ++进程从jni调用android活动)

本文介绍了直接从没有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活动

基础教程推荐