前段时间搞jni,需要java和c++交互。 搜索了很多贴,要不就是不能运行,要不就像某位名人的blog一样自由函数,从来不发代码。直到找到这位哥们的贴,既详细,代码又简单易懂。:http://acoder.me/cocos2d-x-cocos-and-java-chat.html 感谢他,搬运一下希望更多人收益。
代码备份一下:http://download.csdn.net/detail/chenee543216/5864455
一个注意的地方: Android.mk里面需要设定JNI目录,我用的cocos2dx2.1.2版本,在cocos2dx/下面。 自己根据需要修改。不清楚的留言,看到就回。
============================================== 以下zz
今天研究Cocos2d-x,心血来潮的想做个2dx调用Android弹出框的效果.使用jni进行交互有木有.
C++调用Java , Java调用C++.(下载地址瞧下面)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
void
HelloWorld
::
menuCloseCallback
(
CCObject
*
pSender
)
{
JniMethodInfo
jmi
;
if
(
JniHelper
::
getStaticMethodInfo
(
jmi
,
"com/jni/test/JniTest"
,
"sayHello"
,
"([Ljava/lang/String;)V"
)
)
{
jclass
str_cls
=
jmi
.
env
->
FindClass
(
"java/lang/String"
)
;
jstring
str1
=
jmi
.
env
->
NewStringUTF
(
"I'm a titile"
)
;
jstring
str2
=
jmi
.
env
->
NewStringUTF
(
"Are yor exit game?"
)
;
jobjectArray
arrs
=
jmi
.
env
->
NewObjectArray
(
2
,
str_cls
,
0
)
;
jmi
.
env
->
SetObjectArrayElement
(
arrs
,
0
,
str1
)
;
jmi
.
env
->
SetObjectArrayElement
(
arrs
,
1
,
str2
)
;
jmi
.
env
->
CallStaticVoidMethod
(
jmi
.
classID
,
jmi
.
methodID
,
arrs
)
;
}
}
extern
"C"
{
/* 命名规则:Java_Java的包名_类名*/
void
Java_com_jni_test_JniTest_sayHello
(
)
{
CCLog
(
"hello java , i'm c"
)
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package
com
.
jni
.
test
;
import
java
.
io
.
UnsupportedEncodingException
;
import
org
.
cocos2dx
.
lib
.
Cocos2dxActivity
;
import
android
.
app
.
AlertDialog
;
import
android
.
app
.
Dialog
;
import
android
.
content
.
DialogInterface
;
import
android
.
os
.
Bundle
;
import
android
.
os
.
Handler
;
import
android
.
os
.
Message
;
public
class
JniTest
extends
Cocos2dxActivity
{
private
static
AlertDialog
mDialog
=
null
;
private
static
Handler
mHandler
=
new
Handler
(
new
Handler
.
Callback
(
)
{
@Override
public
boolean
handleMessage
(
Message
msg
)
{
String
[
]
str
=
(
String
[
]
)
msg
.
obj
;
mDialog
.
setTitle
(
str
[
0
]
)
;
mDialog
.
setMessage
(
str
[
1
]
)
;
mDialog
.
show
(
)
;
return
true
;
}
}
)
;
public
static
native
void
sayHello
(
)
;
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
mDialog
=
new
AlertDialog
.
Builder
(
this
)
.
create
(
)
;
mDialog
.
setButton
(
"确定"
,
new
AlertDialog
.
OnClickListener
(
)
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
JniTest
.
this
.
finish
(
)
;
}
}
)
;
mDialog
.
setButton2
(
"取消"
,
new
AlertDialog
.
OnClickListener
(
)
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
}
}
)
;
mDialog
.
setButton3
(
"HelloC"
,
new
AlertDialog
.
OnClickListener
(
)
{
@Override
public
void
onClick
(
DialogInterface
dialog
,
int
which
)
{
// Java调用c代码
JniTest
.
this
.
sayHello
(
)
;
}
}
)
;
}
static
{
System
.
loadLibrary
(
"game"
)
;
}
public
static
void
sayHello
(
String
[
]
str
)
{
Message
mes
=
new
Message
(
)
;
mes
.
obj
=
str
;
mHandler
.
sendMessage
(
mes
)
;
}
}
|
效果如下:C++调用Android对话框
Java调用C++打印字符串