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>