Android Jni中使用线程及回调更新UI
来源:互联网 发布:小山竹网络春晚视频 编辑:程序博客网 时间:2024/05/28 11:50
转自:http://blog.csdn.net/geolo/article/details/6167646
Geolo:开个java线程。用jni让java调用c的方法,再通过C调用Java的方法去刷新线程。有点无聊,不过我想要找到是在Android的NDK中,怎么写C/C++的线程。如果有知道的朋友,麻烦告知下。谢谢。
以下是转贴内容:
原文:http://blog.sina.com.cn/s/blog_4b650d650100moda.html
在Android使用Jni时,为了能够使UI线程即主线程与工作线程分开,经常要创建工作线程,然后在工作线程中调用C/C++函数.为了在C/C++函数中更新Android的UI,又时常使用回调。为了保证C/C++的工作函数以及回调函数都能轻易同时被Java的UI线程和创建的工作线程识别,我们声明native时经常要把他们声明成静态函数.但静态函数更新UI又会出现麻烦.为了解决这个问题,本人查阅了相关的一些文档。有些文档中涉及的问题并不是为了解决我要阐述的问题,但利用一下,就把这个问题解决了. 我写了一个例子,来阐述整个过程的思路.
Java代码:
package eagle.test;public class EagleUI extends Activity{ TextView mTextView; MainHandler mMainHandler; static MainHandler mHandler; //------------------------------------------------------ static { System.loadLibrary("EagleZip");// 声明所要调用的库名称 } //------------------------------------------------------ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTextView=(TextView)findViewById(R.id.MyTextView); mMainHandler=new MainHandler(); mHandler=mMainHandler; WorkThread tThread = new WorkThread (); new Thread(tThread).start(); } //------------------------------------------------------ public static void myCallbackFunc(String nMsg) { Message tMsg=new Message(); Bundle tBundle=new Bundle(); tBundle.putString("CMD", nMsg); tMsg.setData(tBundle); mHandler.sendMessage(tMsg); } //------------------------------------------------------ public static native String myJni(String nParam);// 对要调用的方法做本地声明 //------------------------------------------------------ public class zipThread implements Runnable { @Override public void run() { myJni("Eagle is great"); } } //------------------------------------------------------ class MainHandler extends Handler { public MainHandler(){} public MainHandler(Looper L) { super(L); } public void handleMessage(Message nMsg) { super.handleMessage(nMsg); Bundle tBundle=nMsg.getData(); String tCmd=tBundle.getString("CMD"); EagleUI.this.mTextView.setText(tCmd); } }}
C/C++代码:
#include <jni.h>jclass gJniClass;jmethodID gJinMethod;//---------------------------------------------------------------JNIEXPORT jstring JNICALLJava_eagle_test_EagleUI_myJni(JNIEnv* env, jclass cls,jstring param){ char tChar[256]; const char *tpParam; gJniClass = cls; gJinMethod = 0; gJinMethod=(*env)->GetStaticMethodID(env,gJniClass,"myCallbackFunc","(Ljava/lang/String;)V"); if(gJinMethod == 0 || gJinMethod == NULL) return (*env)->NewStringUTF(env, "-2"); strcpy(tChar,"Hello Eagle"); (*env)->CallStaticVoidMethod(env,gJniClass,gJinMethod,(*env)->NewStringUTF(env, tChar)); DisplayCallBack(env,tChar); tpParam =(*env)->GetStringUTFChars(env,param,0); return param;} //---------------------------------------------------------------void DisplayCallBack(JNIEnv* env,char nMsg[]){ char tChars[256]; strcpy(tChars,nMsg); (*env)->CallStaticVoidMethod(env,gJniClass,gJinMethod,(*env)->NewStringUTF(env, tChars)); }
- Android Jni中使用线程及回调更新UI
- Android Jni中使用线程及回调更新UI
- Android Jni中使用线程及回调更新UI
- Android Jni中使用线程及回调更新UI
- Android Jni中使用线程及回…
- Android 线程中通过回调方式更新界面UI.
- Android 线程中更新UI
- Android 非UI线程中更新UI
- Android 非UI线程中更新UI
- android中更新UI线程方法
- Android子线程中更新UI
- Android中使用Thread+Handler实现非UI线程更新UI界面
- 线程中更新ui
- 线程中更新UI
- Android在非UI线程中更新UI的方法
- Android开之在非UI线程中更新UI
- android在非UI线程中更新UI
- Android非UI线程中更新UI界面
- mac系统截屏的快捷键
- NcPaint重绘非客户区
- Android,XML配置文件内容记录
- 一 相关概念的了解
- Java注解Annotation
- Android Jni中使用线程及回调更新UI
- 挖Linux中的古老缩略语
- Axis 开发webservice
- Windows2008 64位系统asp连接access数据库解决方法(转)
- Windows常见性能计数器
- Derby数据库入门
- Mongodb技术沙龙@admaster
- java 验证码
- Jython+zxJDBC使用sys用户连接数据库