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 intTextView = (TextView)
this.findViewById(R.id.inttextview);
42 stringTextView = (TextView)
this.findViewById(R.id.stringtextview);
43 arrayTextView = (TextView)
this.findViewById(R.id.arraytextview);
44 45 //消息处理
46 mHandler =
new Handler()
47 {
48 @Override
49 public void handleMessage(Message msg)
50 {
51 switch(msg.what)
52 {
53 //整型
54 case 0:
55 {
56 intTextView.setText(msg.obj.toString());
57 break;
58 }
59 //字符串
60 case 1:
61 {
62 stringTextView.setText(msg.obj.toString());
63 break;
64 }
65 //数组
66 case 2:
67 {
byte[] b = (
byte[])msg.obj;
68 arrayTextView.setText(Byte.toString(b[0])+Byte.toString(b[1])+Byte.toString(b[2])+Byte.toString(b[3])+Byte.toString(b[4]));
69 break;
70 }
71 }
72 73 }
74 75 };
76 77 78 }
79 80 //按钮监听实现
81 public class ClickListener
implements View.OnClickListener
82 {
83 84 @Override
85 public void onClick(View v)
86 {
87 // TODO Auto-generated method stub
88 switch(v.getId())
89 {
90 case R.id.intbutton:
91 {
92 //调用JNI中的函数
93 callJNIInt(1);
94 break;
95 }
96 case R.id.stringbutton:
97 {
98 //调用JNI中的函数
99 callJNIString("你好A");
100 break;
101 }
102 case R.id.arraybutton:
103 {
104 //调用JNI中的函数
105 callJNIByte(
new byte[]{1,2,3,4,5});
106 break;
107 }
108 }
109 }
110 111 }
112 113 114 //被JNI调用,参数由JNI传入
115 private void callbackInt(
int i)
116 {
117 Message msg =
new Message();
118 //消息类型
119 msg.what = 0;
120 //消息内容
121 msg.obj = i;
122 //发送消息
123 mHandler.sendMessage(msg);
124 }
125 126 //被JNI调用,参数由JNI传入
127 private void callbackString(String s)
128 {
129 Message msg =
new Message();
130 //消息类型
131 msg.what = 1;
132 //消息内容
133 msg.obj = s;
134 //发送消息
135 mHandler.sendMessage(msg);
136 }
137 138 //被JNI调用,参数由JNI传入
139 private void callbackByte(
byte[] b)
140 {
141 Message msg =
new Message();
142 //消息类型
143 msg.what = 2;
144 //消息内容
145 msg.obj = b;
146 //发送消息
147 mHandler.sendMessage(msg);
148 }
149 150 //本地方法,由java调用
151 private native void callJNIInt(
int i);
152 private native void callJNIString(String s);
153 private native void callJNIByte(
byte[] b);
154 155 static156 {
157 //加载本地库
158 System.loadLibrary("myjni");
159 }
160 161 }
1 LOCAL_PATH := $(call my-dir)
2 3 include $(CLEAR_VARS)
4 5 LOCAL_MODULE := myjni
6 LOCAL_SRC_FILES := callback.c
7 8 LOCAL_LDLIBS := -llog
9 10 include $(BUILD_SHARED_LIBRARY)