1. 程式人生 > >安卓開發——JNI——回撥java中的方法

安卓開發——JNI——回撥java中的方法

JNI開發中  

在C程式碼中回撥java中的方法 

package com.example.jnitest2;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

public class HomeActivity extends Activity {

	private NativeProviefder nativeProviefder;
	static{
		System.loadLibrary("jnitest2");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

		nativeProviefder = new NativeProviefder(this);

	}

	//點選呼叫一個空引數的方法 
	public  void showStringOnclick(View view ){

		nativeProviefder.showStringNative();
		//		showStringNative();
	}
	//點選呼叫一個相加的方法
	public  void addNumber(View view ){
		nativeProviefder.addNative();
		//		add();
	}
	//點選呼叫一個打出字串的方法 
	public  void printString(View view ){
		nativeProviefder.printStringNative();
	}
	//點選呼叫一個打出字串的方法 
	public  void printStaticString(View view ){
		nativeProviefder.printStringStaticNative();
	}


}


package com.example.jnitest2;

import android.content.Context;
import android.widget.Toast;

public class NativeProviefder {

	static{
		System.loadLibrary("jnitest2");
	}
	Context context;
	public NativeProviefder(Context context) {
		this.context = context;
	}
	public NativeProviefder() {
		super();
	}
	

	public void showString(){
		String string = "這是一個空的構造C來呼叫來顯示 ";
		System.out.println(string);
		Toast.makeText(context, "這是空的==="+string, 0).show();
	}
	public void adds(int a,int b){
		String string = "這是這是用來計算加法運算的方法 ,計算結果 是 :  ";
		int sum = a+b;
		System.out.println(string+sum);
		Toast.makeText(context, "這是空的==="+sum, 0).show();
		
	}
	public void printStrings(String string ){
		String strings = "這是一個輸出列印一個字條 串的  方法   輸出 的字串 為  : ";
		
		System.out.println(strings);
		Toast.makeText(context, "printString==="+strings +"::"+string, 0).show();
	}
	
	public static void printStaticStrings(){
		String strings = "printStaticStrings: ";
		System.out.println(strings);
	}
	public native void  showStringNative();
	public native void addNative();
	public native void printStringNative();
	public native void printStringStaticNative() ;
	
	
}


#include <jni.h>
#include <stdio.h>
#include <stdlib.h>


//回撥空引數的方法
JNIEXPORT void JNICALL Java_com_example_jnitest2_NativeProviefder_showStringNative
(JNIEnv * env , jobject obj){
	// (*FindClass)(JNIEnv*, const char*);
	jclass  claxx = (*env)->FindClass(env,"com/example/jnitest2/NativeProviefder");
	//jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
	jmethodID  method = (*env)->GetMethodID(env,claxx,"showString","()V");
	(*env)->CallVoidMethod(env,obj,method);

}

//回撥帶int引數的陣列
JNIEXPORT void JNICALL Java_com_example_jnitest2_NativeProviefder_addNative
(JNIEnv * env , jobject obj){
	jclass claxx = (*env)->FindClass(env,"com/example/jnitest2/NativeProviefder");
	jmethodID  method = (*env)->GetMethodID(env,claxx,"adds","(II)V");

	(*env)->CallVoidMethod(env,obj,method,1,2);
}

//回撥帶String引數的方法
JNIEXPORT void JNICALL Java_com_example_jnitest2_NativeProviefder_printStringNative
(JNIEnv * env , jobject obj){
	//載入類的位元組碼
	jclass claxx = (*env)->FindClass(env,"com/example/jnitest2/NativeProviefder");
	jmethodID method= (*env)->GetMethodID(env,claxx,"printStrings","(Ljava/lang/String;)V");
	jstring charst = (*env)->NewStringUTF(env,"yhtjhfhy");
	(*env)->CallVoidMethod(env,obj,method,charst);

}
//回撥靜態方法
JNIEXPORT void JNICALL Java_com_example_jnitest2_NativeProviefder_printStringStaticNative
(JNIEnv * env , jobject obj){
	//載入類的位元組碼
	jclass claxx = (*env)->FindClass(env,"com/example/jnitest2/NativeProviefder");
	jmethodID method= (*env)->GetStaticMethodID(env,claxx,"printStaticStrings","()V");
	(*env)->CallVoidMethod(env,obj,method);
}
//呼叫其他類的方法
//JNIEXPORT void JNICALL Java_com_example_jnitest2_NativeProviefder_printStringStaticNativess
//(JNIEnv * env , jobject obj){
//	//載入類的位元組碼
//	jclass claxx = (*env)->FindClass(env,"com/example/jnitest2/NativeProviefder");
//	jmethodID method= (*env)->GetStaticMethodID(env,claxx,"printStaticStrings","()V");
//	//jobject     (*AllocObject)(JNIEnv*, jclass);
//	jobject mainobi = (*env)->AllocObject(env,"全類名");
//	(*env)->CallVoidMethod(env,mainobi,method);
//}



LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := jnitest2
LOCAL_SRC_FILES := jnitest2.c

include $(BUILD_SHARED_LIBRARY)


頁面配製檔案 

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    
    tools:context=".HomeActivity" >

    <Button
        android:onClick="showStringOnclick"
        android:id="@+id/bt_showstring"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C中回撥一個空的引數" />

    <Button
        android:onClick="addNumber"
        android:id="@+id/bt_addnumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C中回撥相加的方法" />

    <Button
        android:onClick="printString"
        android:id="@+id/bt_printstring"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C中回撥列印字串" />
    <Button
        android:onClick="printStaticString"
        android:id="@+id/bt_printstaticstring"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="C中回撥列印靜態方法 字串" />

</LinearLayout>