Java与C的相互调用

 

java:

package com.nan.callback;

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");
    }
   
}

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 JNICALL Java_com_nan_callback_MyCallbackActivity_callJNIInt( JNIEnv* env, jobject obj , jint i)
{
    //找到java中的类
    jclass cls = (*env)->FindClass(env, "com/nan/callback/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_nan_callback_MyCallbackActivity_callJNIString( JNIEnv* env, jobject obj , jstring s)
{
    //找到java中的类
    jclass cls = (*env)->FindClass(env, "com/nan/callback/MyCallbackActivity");
    //再找类中的方法
    jmethodID mid = (*env)->GetMethodID(env, cls, "callbackString", "(Ljava/lang/String;)V");
    if (mid == NULL)
    {
        LOGI("string error");
        return; 
    }
    const char *ch;
    //获取由java传过来的字符串
    ch = (*env)->GetStringUTFChars(env, s, NULL);
    //打印
    LOGI("from java string: %s",ch);
    (*env)->ReleaseStringUTFChars(env, s, ch);   
    //回调java中的方法
    (*env)->CallVoidMethod(env, obj, mid ,(*env)->NewStringUTF(env,"你好haha"));

}

/********传输数组(byte[])*************
*/
JNIEXPORT void JNICALL Java_com_nan_callback_MyCallbackActivity_callJNIByte( JNIEnv* env, jobject obj , jbyteArray b)
{
    //找到java中的类
    jclass cls = (*env)->FindClass(env, "com/nan/callback/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中的方法
    (*env)->CallVoidMethod(env, obj, mid ,carr);
}

 

Reprinted from:http://blog.csdn.net/vincent_czz/article/details/7688882

posted @ 2012-07-20 17:42  Qiengo  阅读(656)  评论(0编辑  收藏  举报