这个案例是用来实现Java层定时调用JNI层的方法,通过该方法返回来的随机数进行判断,不断调整UI显示效果,可以举一反三.
<1> : 新建一个android工程:
大致思路是这样的:
定义一个Timer定时器,这个定时器可以实现每个多久就开始调用一次任务(TimerTask):
Timer :
Timer timer=new Timer();
timer.schedule(task, 500,1000);
第二个参数是到时间时,再延时500ms开始执行task,第三个参数即是每隔1000ms执行一次task任务.
TimerTask :
TimerTask task=new TimerTask(){ @Override public void run() { // TODO Auto-generated method stub rnd=getLimit(); handler.sendEmptyMessage(0); } };
处理消息并且更新UI:
final Handler handler=new Handler(){ int color; @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); if(rnd<100){ color=Color.GREEN; }else if(rnd<200){ color=Color.BLACK; }else if(rnd<300){ color=Color.YELLOW; }else{ color=Color.RED; } mBtn.setText("random : "+rnd); mText.setBackgroundColor(color); } };
JNI:
static{ System.loadLibrary("jnilibs"); } public native int getLimit();
完整的souce Code如下:
package com.example.hellojnidemo6; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.graphics.Color; import android.view.Menu; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { static{ System.loadLibrary("jnilibs"); } public native int getLimit(); private int rnd; private TextView mText; private Button mBtn; final Handler handler=new Handler(){ int color; @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); if(rnd<100){ color=Color.GREEN; }else if(rnd<200){ color=Color.BLACK; }else if(rnd<300){ color=Color.YELLOW; }else{ color=Color.RED; } mBtn.setText("random : "+rnd); mText.setBackgroundColor(color); } }; TimerTask task=new TimerTask(){ @Override public void run() { // TODO Auto-generated method stub rnd=getLimit(); handler.sendEmptyMessage(0); } }; Timer timer=new Timer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mText=(TextView)findViewById(R.id.hellojni); mBtn=(Button)findViewById(R.id.button1); mText.setText("hello jni !"); mText.setBackgroundColor(Color.CYAN); timer.schedule(task, 500,1000); } @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; } }
直接对这个java文件进行javah转换后,改一下头文件名为header.h,拷贝到jni目录下,开始具体实现.c的编写开发
xml里面一个Button一个TextView.
C文件:
#include<jni.h> #include<android/log.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #include"header.h" //修改日志tag中的值 #define LOG_TAG "CTAG" //日志显示的等级 #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) JNIEXPORT jint JNICALL Java_com_example_hellojnidemo6_MainActivity_getLimit( JNIEnv * env, jobject obj) { return getRand()%300; } int getRand(){ return rand(); }
H头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_example_hellojnidemo6_MainActivity */ #ifndef _Included_com_example_hellojnidemo6_MainActivity #define _Included_com_example_hellojnidemo6_MainActivity #ifdef __cplusplus extern "C" { #endif /* * Class: com_example_hellojnidemo6_MainActivity * Method: getLimit * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_example_hellojnidemo6_MainActivity_getLimit (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
运行结果: