Adnroid ndk开发中原生代码使用java线程

 MainActivity中源码:MainActivity所在有包名为:com.example.ndkthreads直接决定原生代码方法的命名

package com.example.ndkthreads;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private EditText threadEdit;
	private EditText iterationEdit;
	private Button startButton;
	private TextView logView;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		nativeInit();		//初始化原生代码
		threadEdit = (EditText)findViewById(R.id.thread_edit);
		iterationEdit = (EditText)findViewById(R.id.iteration_edit);
		startButton= (Button)findViewById(R.id.button1);
		logView = (TextView)findViewById(R.id.log_view);
		
		
		startButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				int threads = getNumber(threadEdit,0);
				int iterations = getNumber(iterationEdit,0);
				if(threads>0&&iterations>0)
				{
					startThreads(threads,iterations);
				}
				
			}
			
		});
	}
	
	@Override
	protected void onDestroy()
	{
		nativeFree();
		super.onDestroy();
	}
	
	/**
	 * 原生消息回调
	 */
	private void onNativeMessage(final String message)
	{
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				logView.append(message);
				logView.append("\n");
				System.out.println("原生消息回调------------------"+message);
			}
			
		});
	}
	
	private static int getNumber(EditText editText,int defaultValue)
	{
		int value;
		try{
			value = Integer.parseInt(editText.getText().toString());
		}
		catch(NumberFormatException e)
		{
			value = defaultValue;
		}
		return value;
	}
	
	/**
	 * 启动给定个数的线程进行迭代
	 * @param threads
	 * @param iterations
	 */
	private void startThreads(int threads,int iterations)
	{
		
		javaThreads(threads,iterations);
	}
	private void javaThreads(int threads,final int iterations)
	{
		for(int i=0;i


xml:布局文件:



     
    
    
    

    

原生代码:

#include	 //引入Adnroid打印日志文件
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;

/*
#ifndef NULL
#define NULL  ((void *) 0)
#endif
*/

#define LOG    "NDKThreads" 													// 日志打印标识
#define LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG,__VA_ARGS__) 	// 对应Adnroid中DEBUGg模式,下类似
#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG,__VA_ARGS__)
#define LOGW(...)  __android_log_print(ANDROID_LOG_WARN,LOG,__VA_ARGS__)
#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG,__VA_ARGS__)
#define LOGF(...)  __android_log_print(ANDROID_LOG_FATAL,LOG,__VA_ARGS__)

//方法ID
static jmethodID gOnNativeMessage = NULL;

extern "C"
{
	JNIEXPORT void JNICALL Java_com_example_ndkthreads_MainActivity_nativeInit(JNIEnv *env,jobject obj)
	{
		if(gOnNativeMessage==NULL)
		{
			jclass clazz =env->GetObjectClass(obj);

			//获取方法id
			gOnNativeMessage = env->GetMethodID(clazz,"onNativeMessage","(Ljava/lang/String;)V");
			if(gOnNativeMessage==NULL)
			{
				//捕获异常
				jclass exceptionClazz = env->FindClass("java/lang/RuntimeException");
				env->ThrowNew(exceptionClazz,"方法未定义");
			}


		}
	}
}

extern "C"
{
	JNIEXPORT void JNICALL Java_com_example_ndkthreads_MainActivity_nativeFree(JNIEnv *env,jobject obj)
	{

	}
}

extern "C"
{
	JNIEXPORT void JNICALL Java_com_example_ndkthreads_MainActivity_nativeWorker(JNIEnv *env,jobject obj,jint id,jint iterations)
	{
		//循环给定的迭代
		for(jint i=0;iNewStringUTF(message); //转换为c字符串

			//调用原生方法
			env->CallVoidMethod(obj,gOnNativeMessage,messageString);

			//检查是否产生异常
			if(NULL!=env->ExceptionOccurred())
			{
				break;
			}
			sleep(1);
		}
	}
}

小结:

在原生代码中使用java线程时:只需要在java代码中定义一个原生接口,具体的线程对象及方法体都在java中的执行

你可能感兴趣的:(android)