Android JNI-JNIの下の階のCはJava方法を転調します.

8836 ワード

Google公式JNIガイドダウンロード:http://download.csdn.net/detail/muyang_レン/9344131
XMLレイアウト

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lianghuiyong.myapplication.MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
LinearLayout>
Javaファイルコード
package com.example.lianghuiyong.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("test-jni");
    }
    public native void test1();
    public native void test2();

    public String text2string = null;
    public TextView textView1 = null;
    public TextView textView2 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1 =(TextView) findViewById(R.id.text1);
        textView2 =(TextView) findViewById(R.id.text2);

        test1();//JNI  settextview    textView  
        test2();//JNI  text2string    text2string  
        textView2.setText(text2string);
    }

    public void settextview(String string){
        textView1.setText(string);
    }
}
JNI常用タイプ対応安卓JNI--JNI底层C回调Java方法_第1张图片
JNI配列タイプ対応安卓JNI--JNI底层C回调Java方法_第2张图片
JNI Get MethodIDの4番目のパラメータは安卓JNI--JNI底层C回调Java方法_第3张图片を参照してください.
JNIファイル
#include 
#include 
#include 


JNIEXPORT void JNICALL
Java_com_example_lianghuiyong_myapplication_MainActivity_test1(JNIEnv *env, jobject instance) {

    // TODO
    //  com.example.lianghuiyong.myapplication.MainActivity  JNI     env  
    jclass mainactivity = (*env)->FindClass(env ,"com/example/lianghuiyong/myapplication/MainActivity");

    //jmethodID:        ID,  MainActivity  settextview  ,
    //     V Java settextview       ,Ljava/lang/String;     
    jmethodID textview1 = (*env)->GetMethodID(env,mainactivity,"settextview","(Ljava/lang/String;)V");

    jstring hello = (*env)->NewStringUTF(env,"hello jni test1");
    (*env)->CallVoidMethod(env,instance,textview1,hello);
}

JNIEXPORT void JNICALL
Java_com_example_lianghuiyong_myapplication_MainActivity_test2(JNIEnv *env, jobject instance) {

    // TODO
    //test2         Java   textView2 ,  textView2 setText()   ,  JNI        。
    //              text2string  ,      Java      ,  handler bundle   。             。
    //jclass:      env  
    //  com.example.lianghuiyong.myapplication.MainActivity  JNI     env  
    jclass mainactivity = (*env)->FindClass(env ,"com/example/lianghuiyong/myapplication/MainActivity");

    //jfieldID:        ID
    jfieldID textview2 = (*env)->GetFieldID(env,mainactivity,"text2string","Ljava/lang/String;");

    jstring hello = (*env)->NewStringUTF(env,"hello jni test2");
    (*env)->SetObjectField(env,instance,textview2,hello);
}
安卓JNI--JNI底层C回调Java方法_第4张图片
Android studio配置JNI:http://blog.csdn.net/muyang_ren/articale/detail/50160241