- 学习JNI使用JNI实现调用Java方法跳转页面
jni的项目结构如下图, CMakeList.txt文件的作用与内用结构 CMake是一个跨平台的构建工具,可以用简单的语句来描述所有平台的安装(编译过程)。能够输出各种各样的makefile或者project文件。Cmake 并不直接建构出最终的软件,而是产生其他工具的脚本(如Makefile ),然后再依这个工具的构建方式使用。 CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。从而达到跨平台的目的。Android Studio利用 CMake生成的是ninja,ninja是一个小型的关注速度的构建系统。我们不需要关心ninja的脚本,知道怎么配置cmake就可以了。从而可以看出cmake其实是一个跨平台的支持产出各种不同的构建脚本的一个工具。 CMake的脚本名默认是CMakeLists.txt。
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
#add_library( # Sets the name of the library.
# native-lib
# # Sets the library as a shared library.
# SHARED
# # Provides a relative path to your source file(s).
# jni/native-lib.cpp)
# 添加jni库
add_library(
ExJNI
SHARED
jni/ExJNI.cpp)
add_library(
Log
SHARED
jni/Log.cpp)
include_directories(${CMAKE_SOURCE_DIR}/base/)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
# native-lib
ExJNI
Log
# Links the target library to the log library
# included in the NDK.
${log-lib} )
在Java代码中如何使用ExJNI.cpp库
public class ExJNI {
static {
System.loadLibrary("ExJNI");
}
public native void takePhoto(Context context);
public native void recordVideo(Context context);
private void toActivity(Context context,Class c){
context.startActivity(new Intent(context,c));
}
}
在activity中实例化ExJNI ,调用两个jni底层原生方法实现跳转页面到拍照和录制视频
ExJNI ex = new ExJNI ();
ex.takePhoto(this)
ex.recordVideo(this)
ExJNI.cpp库代码
#include <jni.h>
#include <string>
#include <pthread.h>
#include <Log.h>
#define takePhotoActivity "org/yxjme/exjni_two/a/TakePhotoActivity"
#define recordVideoActivity "org/yxjme/exjni_two/a/RecordVideoActivity"
extern "C" JNIEXPORT jstring JNICALL
Java_org_yxjme_exjni_1two_ExJNI_getMessage(JNIEnv * env,jobject thiz){
std::string hh = "000000000000000000000000000";
LOGI("[getMessage]->",env->NewStringUTF(hh.c_str()));
jclass _jclass = env->GetObjectClass(thiz);
jmethodID _MethodID = env->GetMethodID(_jclass, "showFiled", "(Ljava/lang/String;)V");
jstring resultMsgStr = env->NewStringUTF("this is Success");
env->CallVoidMethod(thiz,_MethodID,resultMsgStr) ;
return env->NewStringUTF(hh.c_str());
}
extern "C"
JNIEXPORT jobject JNICALL
Java_org_yxjme_exjni_1two_ExJNI_GetOBJ(JNIEnv *env, jobject thiz, jstring jstr) {
jclass _jclass = env->FindClass("org/yxjme/exjni_two/ExJNI") ;
jmethodID mId = env->GetMethodID(_jclass, "<init>", "(Ljava/lang/String;)V");
jobject obj = env->NewObject(_jclass, mId,jstr);
return obj;
}
extern "C"
JNIEXPORT jint JNICALL
Java_org_yxjme_exjni_1two_ExJNI_arrayLength(JNIEnv *env,
jobject thiz,
jintArray array_list) {
int len = env ->GetArrayLength(array_list) ;
LOGI("len = ",len);
jclass cls = env->FindClass("org/yxjme/exjni_two/ExJNI");
jmethodID mID = env->GetMethodID(cls, "fun", "(I)Ljava/lang/String;");
env->CallObjectMethod(thiz,mID,10000) ;
return len ;
}
extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_createThread(JNIEnv *env, jobject thiz) {
}
void * printHelloThread(void *){
LOGI("hello thread");
return nullptr;
}
struct threadArgs{
int code ;
const char *result ;
};
void toActivityMethod(JNIEnv *pEnv, jobject pJobject, jobject pJobject1, const char string[1]);
void * threadArgsBean(void *arg){
threadArgs * args = static_cast<threadArgs *>(arg) ;
LOGI("this is code %d",args->code);
LOGI("this is result %s",args->result);
return nullptr ;
}
extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_createJNIThread(JNIEnv *env, jobject thiz) {
pthread_t handle;
threadArgs *thread = new threadArgs ;
thread->code = 0 ;
thread->result = "传递成功";
int ret = pthread_create(&handle,nullptr,printHelloThread,threadArgsBean(thread)) ;
if(ret == 0 ){
LOGD("create thread success") ;
}else{
LOGD("create thread failed") ;
}
}
extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_takePhoto(JNIEnv *env, jobject thiz,jobject obj) {
toActivityMethod(env,thiz,obj,takePhotoActivity);
}
extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_recordVideo(JNIEnv *env, jobject t,jobject obj) {
toActivityMethod(env,t,obj,recordVideoActivity);
}
void toActivityMethod(JNIEnv *pEnv, jobject pJobject, jobject pJobject1, const char str[1]) {
jclass c = pEnv->FindClass("org/yxjme/exjni_two/ExJNI");
jmethodID mId = pEnv->GetMethodID(c, "toActivity","(Landroid/content/Context;Ljava/lang/Class;)V");
jclass _jcls = pEnv->FindClass(str);
pEnv->CallVoidMethod(pJobject,mId,pJobject1,_jcls) ;
}
使用反射机制跳转activity界面
private void toRecord() {
Class<? extends ExJNI> cls = exJNI.getClass();
try {
ExJNI obj = cls.newInstance();
Method m = cls.getDeclaredMethod(
"toActivity", Context.class, Class.class);
m.setAccessible(true);
m.invoke(exJNI, MainActivity.this, RecordVideoActivity.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
|