欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入淺析jni中的java接口使用

 更新時間:2020年09月11日 14:22:44   作者:程序員老張  
這篇文章主要介紹了jni中的java接口使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

JNI中的java接口使用

項目需求,需要在c++函數(shù)中監(jiān)聽相應(yīng)的狀態(tài),并在java端進行一些列的處理。

這個需要在JNI中寫一個subscriber,注冊后在需要的地方進行引入使用。

目錄結(jié)構(gòu)

初始化是AS上的c++工程文件,這邊先暫時實現(xiàn)簡單的demo,CdemoActivity是NativeActivity的實現(xiàn),我們暫時別管,因為實現(xiàn)是c++層控制的,有興趣可以去百度下

主要涉及jnicallback等c文件和JNIUtil這java文件

JNIUtil

public class JNIUtil {

  static {

    System.loadLibrary("native-event");
  }

  // 注冊函數(shù)
  public static native void setJniCallBack(JniCallBack callBack);

  // 解注冊函數(shù)
  public static native void unJniCallBack();

  public interface JniCallBack{

    void onReceiveCInfo();

  }

}

jnicallback.h

//
// Created by 86130 on 2020/9/9.
//

#ifndef CDEMO_JNICALLBACK_H
#define CDEMO_JNICALLBACK_H


#ifdef __cplusplus
extern "C"{
#endif

//方法回調(diào)
void onReceiveMsg();


#ifdef __cplusplus
}
#endif
#endif //CDEMO_JNICALLBACK_H

jnicallback.cpp

#include <jni.h>
#include <malloc.h>
#include <cstring>

#include "jnicallback.h"

#ifdef __cplusplus
extern "C"{
#endif

  JavaVM *g_VM;
  jobject subscriber;

extern "C"
JNIEXPORT void JNICALL Java_com_demo_cdemo_JNIUtil_setJniCallBack(JNIEnv* env,jclass clazz, jobject call_back) {

  env->GetJavaVM(&g_VM);
  subscriber = env->NewGlobalRef(call_back);

}

extern "C"
JNIEXPORT void JNICALL Java_com_demo_cdemo_JNIUtil_unJniCallBack(JNIEnv* env, jclass clazz) {

  if (subscriber != NULL)
  {
    env->DeleteGlobalRef(subscriber);
  }
}

JNIEnv *getEnv()
{
  JNIEnv *env;
  if (g_VM ==NULL)
  {
    return NULL;
  }
  int envStat = g_VM->GetEnv((void **) &env, JNI_VERSION_1_6);
  if (envStat == JNI_EDETACHED)
  {
    if (g_VM->AttachCurrentThread(&env, NULL) != 0)
    {
      return NULL;
    }
  }
  return env;
}

jmethodID getMethodIdByNameAndSig(JNIEnv *env, const char *name, const char *sig)
{
  if (env == NULL || subscriber == NULL)
  {
    return NULL;
  }
  jclass subscriberClass = env->GetObjectClass(subscriber);
  if (subscriber == 0)
  {
    return NULL;
  }
  jmethodID methodId = env->GetMethodID(subscriberClass, name, sig);
  if (methodId == 0)
  {
    return NULL;
  }
  return methodId;
}

// 頭文件方法實現(xiàn)
void onReceiveMsg()
{
  JNIEnv *env = getEnv();
  jmethodID onReceiveMethodId = getMethodIdByNameAndSig(env, "onReceiveCInfo", "()V");
  if (onReceiveMethodId == NULL)
  {
    return;
  }
  env->CallVoidMethod(subscriber, onReceiveMethodId);

}

#ifdef __cplusplus
}
#endif

在其他的cpp文件中引入jnicallback的頭文件就可以使用相應(yīng)的方法。

CMake文件

project(Native-Activity)

cmake_minimum_required(VERSION 3.4.1)

#引入native_app_glue頭文件
include_directories(F:/AndroidSdk/Sdk/ndk-bundle/sources/android/native_app_glue)

add_library(native-activity SHARED
    main.cpp
     jnicallback.cpp)

add_library(native-event SHARED jnicallback.cpp)

find_library(log-lib log)
find_library(OPENGLES3_LIBRARY GLESv3 "OpenGL ES v3.0 library")
find_library(EGL_LIBRARY EGL "EGL 1.4 library")
find_library(android-lib android)

#編譯為靜態(tài)庫
add_library(app_glue STATIC
    android_native_app_glue.c)

target_link_libraries(native-event
    ${log-lib} #鏈接log庫
    ${android-lib} #鏈接android庫
    )

target_link_libraries(native-activity
    app_glue #鏈接靜態(tài)庫native_app_glue
    ${log-lib} #鏈接log庫
    ${android-lib} #鏈接android庫
    ${OPENGLES3_LIBRARY} #鏈接OpenGLES庫
    ${EGL_LIBRARY} #鏈接EGL庫
    )

JNIUtil的使用

package com.demo.cdemo;

import android.app.NativeActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
public class CdemoActivity extends NativeActivity {

  static {
    System.loadLibrary("native-activity");
  }

  boolean isFirst = false;

  @Override 
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    JNIUtil.setJniCallBack(new JNIUtil.JniCallBack() {
      @Override
      public void onReceiveCInfo() {
        boolean isMain = isMainThread();
        Log.d("zzkk",
            "CdemoActivity onReceiveCInfo isMain = " + isMain);
        if (!isFirst)
        {
          isFirst = true;
          runOnUiThread(new Runnable() {
            @Override public void run() {
              Intent intent = new Intent(CdemoActivity.this
                  , MainActivity.class);
              startActivity(intent);
            }
          });
        }
      }
    });

  }

  private boolean isMainThread() {
    return Looper.getMainLooper() == Looper.myLooper();
  }

}

可以看見onReceiveCInfo這行日志的打印

綜上

到此這篇關(guān)于jni中的java接口使用的文章就介紹到這了,更多相關(guān)java接口使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 數(shù)據(jù)流之Broadcast State

    Java 數(shù)據(jù)流之Broadcast State

    這篇文章主要介紹了Java 數(shù)據(jù)流之Broadcast State,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Spring中@Value注解的三種使用方式詳解

    Spring中@Value注解的三種使用方式詳解

    這篇文章主要介紹了Spring中@Value注解的三種使用方式詳解,文章通過示例代碼非常詳細地介紹,對于每個人的學(xué)習(xí)或工作都有一定的學(xué)習(xí)價值,需要的朋友可以參考下
    2023-08-08
  • Java 利用枚舉實現(xiàn)接口進行統(tǒng)一管理

    Java 利用枚舉實現(xiàn)接口進行統(tǒng)一管理

    這篇文章主要介紹了Java 利用枚舉實現(xiàn)接口進行統(tǒng)一管理,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Java如何獲取主機的基本信息詳解

    Java如何獲取主機的基本信息詳解

    最近遇到一個工作需求,上網(wǎng)查了一下怎樣在Java中獲取本機的ip和主機名,所以下面這篇文章主要給大家介紹了關(guān)于Java如何獲取主機的基本信息,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • springboot學(xué)習(xí)之Thymeleaf模板引擎及原理介紹

    springboot學(xué)習(xí)之Thymeleaf模板引擎及原理介紹

    本文主要介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個高級語言的模板引擎,他的這個語法更簡單而且功能更強大,對springboot?Thymeleaf模板引擎相關(guān)知識感興趣的朋友一起看看吧
    2022-02-02
  • SpringBoot集成easy-rules規(guī)則引擎流程詳解

    SpringBoot集成easy-rules規(guī)則引擎流程詳解

    這篇文章主要介紹了SpringBoot集成easy-rules規(guī)則引擎流程,合理的使用規(guī)則引擎可以極大的減少代碼復(fù)雜度,提升代碼可維護性。業(yè)界知名的開源規(guī)則引擎有Drools,功能豐富,但也比較龐大
    2023-03-03
  • Java源碼刨析之ArrayDeque

    Java源碼刨析之ArrayDeque

    ArrayDeque是Deque接口的一個實現(xiàn),使用了可變數(shù)組,所以沒有容量上的限制。同時,?ArrayDeque是線程不安全的,在沒有外部同步的情況下,不能再多線程環(huán)境下使用<BR>
    2022-07-07
  • java中加密的實現(xiàn)方法(MD5,MD2,SHA)

    java中加密的實現(xiàn)方法(MD5,MD2,SHA)

    這篇文章主要介紹了java中加密的實現(xiàn)方法(MD5,MD2,SHA)的相關(guān)資料,這里提供三種實現(xiàn)加密的方法,大家可以對比一下,需要的朋友可以參考下
    2017-08-08
  • Spring?Boot中自動執(zhí)行sql腳本的方法實例

    Spring?Boot中自動執(zhí)行sql腳本的方法實例

    在SpringBoot的架構(gòu)中,DataSourceInitializer類可以在項目啟動后初始化數(shù)據(jù),我們可以通過自動執(zhí)行自定義sql腳本初始化數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Spring?Boot中自動執(zhí)行sql腳本的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java獲取泛型實際類型的方法詳解

    Java獲取泛型實際類型的方法詳解

    這篇文章主要介紹了Java獲取泛型實際類型的方法詳解,泛型,即“參數(shù)化類型”,一提到參數(shù),最熟悉的就是定義方法時有形參列表,普通方法的形參列表中,每個形參的數(shù)據(jù)類型是確定的,而變量是一個參數(shù),需要的朋友可以參考下
    2023-11-11

最新評論