c用java_Java与C的相互调用

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 classMyCallbackActivity 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.*/@Overridepublic voidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

intButton= (Button)this.findViewById(R.id.intbutton);//注册按钮监听

intButton.setOnClickListener(newClickListener());

stringButton= (Button)this.findViewById(R.id.stringbutton);//注册按钮监听

stringButton.setOnClickListener(newClickListener());

arrayButton= (Button)this.findViewById(R.id.arraybutton);//注册按钮监听

arrayButton.setOnClickListener(newClickListener());

intTextView= (TextView)this.findViewById(R.id.inttextview);

stringTextView= (TextView)this.findViewById(R.id.stringtextview);

arrayTextView= (TextView)this.findViewById(R.id.arraytextview);//消息处理

mHandler = newHandler()

{

@Overridepublic voidhandleMessage(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 classClickListener implements View.OnClickListener

{

@Overridepublic voidonClick(View v)

{//TODO Auto-generated method stub

switch(v.getId())

{caseR.id.intbutton:

{//调用JNI中的函数

callJNIInt(1);break;

}caseR.id.stringbutton:

{//调用JNI中的函数

callJNIString("你好A");break;

}caseR.id.arraybutton:

{//调用JNI中的函数

callJNIByte(new byte[]{1,2,3,4,5});break;

}

}

}

}//被JNI调用,参数由JNI传入

private void callbackInt(inti)

{

Message msg= newMessage();//消息类型

msg.what = 0;//消息内容

msg.obj =i;//发送消息

mHandler.sendMessage(msg);

}//被JNI调用,参数由JNI传入

private voidcallbackString(String s)

{

Message msg= newMessage();//消息类型

msg.what = 1;//消息内容

msg.obj =s;//发送消息

mHandler.sendMessage(msg);

}//被JNI调用,参数由JNI传入

private void callbackByte(byte[] b)

{

Message msg= newMessage();//消息类型

msg.what = 2;//消息内容

msg.obj =b;//发送消息

mHandler.sendMessage(msg);

}//本地方法,由java调用

private native void callJNIInt(inti);private native voidcallJNIString(String s);private native void callJNIByte(byte[] b);static{//加载本地库

System.loadLibrary("myjni");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值