当前位置:编程学习 > JAVA >>

Android NDK开发(1)----- Java与C互相调用实例详解

一、概述
      对于大部分应用开发者来说可能都不怎么接触到NDK,但如果涉及到硬件操作的话就不得不使用NDK了。使用NDK还有另一个原因,就是C/C++的效率比较高,因此我们可以把一些耗时的操作放在NDK中实现。
      关于java与c/c++的互相调用,网上有一大堆的文章介绍。但仔细观察可以发现,基本都是讲在java中调用一个本地方法,然后由该本地方法直接返回一个参数给java(例如,在java中定义的本地方法为private int callJNI(int i))。但在大多数时候要求的并不是由开发者在java层主动去调JNI中的函数来返回想要的数据,而是由JNI主动去调java中的函数。举个最简单的例子,Android中的Camera,图像数据由内核一直往上传到java层,然而这些数据的传递并不需要开发者每一次主动去调用来JNI中的函数来获取,而是由JNI主动传给用java中方法,这类似于Linux驱动机制中的异步通知。

二、要求
      用NDK实现Java与C/C++互调,实现int,string,byte[]这三种类型的互相传递。

三、实现
      下面的实现中,每次java调用JNI中的某个函数时,最后会在该函数里回调java中相应的方法而不是直接返回一个参数。可能你会觉得这不还是每次都是由开发者来主动调用吗,其实这只是为了讲解而已,在实际应用中,回调java中的方法应该由某个事件(非java层)来触发。
      新建工程MyCallback,修改main.xml文件,在里面添加3个Button,分别对应3种类型的调用和3个TextView分别显示由JNI回调java时传给java的数据。完整的main.xml文件如下:
 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6
 7     <Button
 8         android:id="@+id/intbutton"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="传给JNI一个整数1"
12         />
13    
14     <TextView
15         android:id="@+id/inttextview"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:text="接收到的整数:"
19         />
20    
21     <Button
22         android:id="@+id/stringbutton"
23         android:layout_width="fill_parent"
24         android:layout_height="wrap_content"
25         android:text="传给JNI一个字符A"
26         />
27    
28     <TextView
29         android:id="@+id/stringtextview"
30         android:layout_width="fill_parent"
31         android:layout_height="wrap_content"
32         android:text="接收到的字符:"
33         />
34    
35     <Button
36         android:id="@+id/arraybutton"
37         android:layout_width="fill_parent"
38         android:layout_height="wrap_content"
39         android:text="传给JNI一个数组12345"
40         />
41    
42     <TextView
43         android:id="@+id/arraytextview"
44         android:layout_width="fill_parent"
45         android:layout_height="wrap_content"
46         android:text="接收到的数组:"
47         />
48    
49
50 </LinearLayout>

修改MyCallbackActivity.java文件,定义了一个Handler,当JNI回调java的方法时,用来发送消息;实现3个Button的监听。如下:
 
  1 package com.nan.callback;
  2
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.os.Handler;
  6 import android.os.Message;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.TextView;
 10
 11
 12 public class MyCallbackActivity extends Activity
 13 {
 14     private Button intButton = null;
 15     private Button stringButton = null;
 16     private Button arrayButton = null;
 17     private TextView intTextView = null;
 18     private TextView stringTextView = null;
 19     private TextView arrayTextView = null;
 20    
 21     private Handler mHandler = null;
 22    
 23    
 24     /** Called when the activity is first created. */
 25     @Override
 26     public void onCreate(Bundle savedInstanceState)
 27     {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.main);
 30        
 31         intButton = (Button)this.findViewById(R.id.intbutton);
 32         //注册按钮监听
 33         intButton.setOnClickListener(new ClickListener());
 34         stringButton = (Button)this.findViewById(R.id.stringbutton);
 35         //注册按钮监听
 36         stringButton.setOnClickListener(new ClickListener());
 37         arrayButton = (Button)this.findViewById(R.id.arraybutton);
 38         //注册按钮监听
 39         arrayButton.setOnClickListener(new ClickListener());
 40        
 41    &n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,