直接从没有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活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)