package com.demo.jniparamstrans;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MyCallbackActivity extends Activity
{
private Button intButton = null;
private Button stringButton = null;
private Button arrayButton = null;
private TextView intTextView = null;
private TextView stringTextView = null;
private TextView arrayTextView = null;
private Handler mHandler = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intButton = (Button)this.findViewById(R.id.intbutton);
//註冊按鈕監聽
intButton.setOnClickListener(new ClickListener());
stringButton = (Button)this.findViewById(R.id.stringbutton);
//註冊按鈕監聽
stringButton.setOnClickListener(new ClickListener());
arrayButton = (Button)this.findViewById(R.id.arraybutton);
//註冊按鈕監聽
arrayButton.setOnClickListener(new ClickListener());
intTextView = (TextView)this.findViewById(R.id.inttextview);
stringTextView = (TextView)this.findViewById(R.id.stringtextview);
arrayTextView = (TextView)this.findViewById(R.id.arraytextview);
//消息處理
mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
//整型
case 0:
{
intTextView.setText(msg.obj.toString());
break;
}
//字符串
case 1:
{
stringTextView.setText(msg.obj.toString());
break;
}
//數組
case 2:
{ byte[] b = (byte[])msg.obj;
arrayTextView.setText(Byte.toString(b[0])+Byte.toString(b[1])+Byte.toString(b[2])+Byte.toString(b[3])+Byte.toString(b[4]));
break;
}
}
}
};
}
//按鈕監聽實現
public class ClickListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.intbutton:
{
//調用JNI中的函數
callJNIInt(1);
break;
}
case R.id.stringbutton:
{
//調用JNI中的函數
callJNIString("你好A");
break;
}
case R.id.arraybutton:
{
//調用JNI中的函數
callJNIByte(new byte[]{1,2,3,4,5});
break;
}
}
}
}
//被JNI調用,參數由JNI傳入
private void callbackInt(int i)
{
Message msg = new Message();
//消息類型
msg.what = 0;
//消息內容
msg.obj = i;
//發送消息
mHandler.sendMessage(msg);
}
//被JNI調用,參數由JNI傳入
private void callbackString(String s)
{
Message msg = new Message();
//消息類型
msg.what = 1;
//消息內容
msg.obj = s;
//發送消息
mHandler.sendMessage(msg);
}
//被JNI調用,參數由JNI傳入
private void callbackByte(byte[] b)
{
Message msg = new Message();
//消息類型
msg.what = 2;
//消息內容
msg.obj = b;
//發送消息
mHandler.sendMessage(msg);
}
//本地方法,由java調用
private native void callJNIInt(int i);
private native void callJNIString(String s);
private native void callJNIByte(byte[] b);
static
{
//加載本地庫
System.loadLibrary("myjni");
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/intbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="傳給JNI一個整數1"
/>
<TextView
android:id="@+id/inttextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="接收到的整數:"
/>
<Button
android:id="@+id/stringbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="傳給JNI一個字符A"
/>
<TextView
android:id="@+id/stringtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="接收到的字符:"
/>
<Button
android:id="@+id/arraybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="傳給JNI一個數組12345"
/>
<TextView
android:id="@+id/arraytextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="接收到的數組:"
/>
</LinearLayout>
callback.c:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <jni.h>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
/**********傳輸整數*************
*/
JNIEXPORT void Java_com_demo_jniparamstrans_MyCallbackActivity_callJNIInt(
JNIEnv* env, jobject obj, jint i) {
LOGI("Int Begin");
//找到java中的類
jclass cls = (*env)->FindClass(env,
"com/demo/jniparamstrans/MyCallbackActivity");
//再找類中的方法
jmethodID mid = (*env)->GetMethodID(env, cls, "callbackInt", "(I)V");
if (mid == NULL) {
LOGI("int error");
return;
}
//打印接收到的數據
LOGI("from java int: %d", i);
//回調java中的方法
(*env)->CallVoidMethod(env, obj, mid, i);
}
/********傳輸字符串*************
*/
JNIEXPORT void JNICALL Java_com_demo_jniparamstrans_MyCallbackActivity_callJNIString( JNIEnv* env, jobject obj , jstring s)
{
//找到java中的類
jclass cls = (*env)->FindClass(env, "com/demo/jniparamstrans/MyCallbackActivity");
//再找類中的方法
jmethodID mid = (*env)->GetMethodID(env, cls, "callbackString", "(Ljava/lang/String;)V");
if (mid == NULL)
{
LOGI("string error");
return;
}
const char *ch;
//獲取由java傳過來的字符串
//GetStringUTFChars將jstring轉換成爲UTF-8格式的char*
ch = (*env)->GetStringUTFChars(env, s, NULL);
//打印
LOGI("from java string: %s", ch);
//ReleaseStringUTFChars釋放指向UTF-8格式的char*的指針
(*env)->ReleaseStringUTFChars(env, s, ch);
//回調java中的方法
(*env)->CallVoidMethod(env, obj, mid ,(*env)->NewStringUTF(env,"你好haha"));
}
/********傳輸數組(byte[])*************
*/
JNIEXPORT void JNICALL Java_com_demo_jniparamstrans_MyCallbackActivity_callJNIByte( JNIEnv* env, jobject obj , jbyteArray b)
{
//找到java中的類
jclass cls = (*env)->FindClass(env, "com/demo/jniparamstrans/MyCallbackActivity");
//再找類中的方法
jmethodID mid = (*env)->GetMethodID(env, cls, "callbackByte", "([B)V");
if (mid == NULL)
{
LOGI("byte[] error");
return;
}
//獲取數組長度
jsize length = (*env)->GetArrayLength(env,b);
LOGI("length: %d",length);
//獲取接收到的數據
int i;
jbyte* p = (*env)->GetByteArrayElements(env,b,NULL);
//打印
for(i=0;i<length;i++)
{
LOGI("%d",p[i]);
}
char c[5];
c[0] = 1;c[1] = 2;c[2] = 3;c[3] = 4;c[4] = 5;
//構造數組
jbyteArray carr = (*env)->NewByteArray(env,length);
(*env)->SetByteArrayRegion(env,carr,0,length,c);
//回調java中的方法, CallXXXMethod 表示對 XXX 返回類型的 Field 函數進行調用
(*env)->CallVoidMethod(env, obj, mid ,carr);
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myjni
LOCAL_SRC_FILES := callback.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
參考了一些網上的內容,記不住網址了,在此表示感謝!