Java基礎知識JNI 在Android中使用System.loadLibrary()__java

來源:互聯網
上載者:User

tiny6410中的部分硬體(串口、led、pwm蜂鳴器、adc等) 友善做了一個硬體庫friendlyarm-hardware.so

如果android應用程式想要訪問以上硬體時,就要使用java的JNI技術


1、JNI的全稱為:Java Native Interface,java本地介面,設計目的是與C/C++實現本地互動,在java辦不到的或者使用方便的程式碼片段調用。

2、JNI的編寫步驟

a、編寫帶有native聲明的方法的java類

b、使用javac命令編譯a中實現的類

c、javah -jni java類名產生副檔名為h的標頭檔

d、使用C/C++實現本地方法

e、將d中的本地方法產生動態連結程式庫

f、OK


1、編寫java程式

class HelloWorld{
public native void displayHelloWorld();
static{
System.loadLibrary("hello");
}
public static void main(String[] args){
new HelloWorld().displayHelloWorld();
}
}

javac hello.java

然後javah命令

javah -jni HelloWorld

產生.h標頭檔為

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */


#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    displayHelloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
  (JNIEnv *, jobject);


#ifdef __cplusplus
}
#endif
#endif



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.