C++调用Android 与Android调用C++ 例子
2013-09-14 16:45
190 查看
现在我们在Android上玩到的游戏,大都是由C++编写的,然后通过NDK编译,才能运行在Android上。而C++与Android之间的交互,通过NDK这个编译工具。那么C++与Android之间是如何交互的?
Android调用C++,我们通过从C++返回一个String来作为例子。
C++调用Android,这里通过弹出一个提示框。
Android工程名:com.example.cocos2dinput
Activity名:MainActivity
首先是Android层的MainActivity源代码:
下面是jni.cpp
Android.mk
记得用NDK编译,编译命令是:
上我们的效果图:
Android调用C++,我们通过从C++返回一个String来作为例子。
C++调用Android,这里通过弹出一个提示框。
Android工程名:com.example.cocos2dinput
Activity名:MainActivity
首先是Android层的MainActivity源代码:
public class MainActivity extends Activity { TextView ContentTextView; Button buttonCallC; String contentString; public static Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentString=getStringFromC(); ContentTextView=(TextView)findViewById(R.id.text1); ContentTextView.setText(contentString); button=(Button)findViewById(R.id.button1); buttonCallC=(Button)findViewById(R.id.button2); mContext=this.getApplicationContext(); buttonCallC.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub callShowMessage(); } }); } public void showMessage() { Log.d("showMessage", "showMessage"); AlertDialog.Builder builder=new Builder(this); builder.setTitle("C++调用Android"); builder.setMessage("这是一个C++调用Android的例子"); builder.show(); } public native String callShowMessage(); public native String getStringFromC(); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } static{ System.loadLibrary("cocos2dinput"); } }
下面是jni.cpp
#include<string.h> #include<jni.h> #include<android/log.h> JNIEnv *g_env; jobject *g_object; extern "C" { JNIEXPORT jstring JNICALL Java_com_example_cocos2dinput_MainActivity_getStringFromC(JNIEnv* env,jobject thiz) { return env->NewStringUTF("callCMessageBox"); } //下面的函数首先被Android调用然后在函数里面又调用了Java JNIEXPORT jint JNICALL Java_com_example_cocos2dinput_MainActivity_callShowMessage(JNIEnv* env,jobject thiz) { jmethodID notification_method = env->GetMethodID(env->GetObjectClass(thiz),"showMessage","()V"); env->CallVoidMethod(thiz,notification_method); return 0; } }
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_LDLIBS := -llog LOCAL_MODULE := cocos2dinput LOCAL_SRC_FILES :=./jni.cpp include $(BUILD_SHARED_LIBRARY)
记得用NDK编译,编译命令是:
ndk-build
上我们的效果图:
相关文章推荐
- Android JNI C/C++调用Java方法
- Android NDK JNI: C++如何调用Java API传递字符串, 并将字符串显示到TextView.
- Android JNI通过C++调用JAVA
- Android 中Java 和C/C++的相互调用方法
- android ndk c/c++ 调用java
- Android C++调用JAVA
- Android 学习笔记——利用JNI技术在Android中调用、调试C++代码
- Java使用JNI调用C/C++(windows和android平台)
- AndroidJNI 通过C++调用JAVA
- android使用JNI调用C,C++
- AndroidJNI 通过C++调用JAVA
- OpenCV4Android开发之旅(三)----Windows平台Eclipse、MinGW配置OpenCV2.4.4(C++接口调用) 全过程(附:MinGW编译OpenCV2.4.4)
- android-JNI学习之java和c++相互调用
- AndroidJNI 通过C++调用JAVA
- Cocos2dx中Android部分的c++和java实现相互调用(高级篇)
- Android JNI通过C++调用JAVA
- Android下C++代码调用汇编函数
- Android调用C++OpenCV程序的方法(NDK+JNI)
- Android中使用JNI调用C/C++
- 在Android中,通过JNI实现C++与Java相互调用