Android NDK JNI开发<9>

本文介绍如何在Java层通过定时器调度JNI层的方法,利用返回的随机数动态调整UI显示效果,实现交互式用户体验增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个案例是用来实现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


运行结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值