JNI开发之锅炉压力监控器

这个例子主要是演示了JNI在实际开发中的开发流程。在实际开发中,android工程师只需要从C/C++工程师那里

拿到底层的一些逻辑代码,整合到jni目录下的.c文件即可


代码的链接地址:http://download.csdn.net/detail/caihongshijie6/6651355

一、原理图



二、效果图




三、代码实现

1、MyView

package com.njupt.monitor;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

public class MyView extends View {

	private int bottom;
	private Paint paint;
	
	public MyView(Context context,int bottom,int color) {
		super(context);
		
		this.bottom = bottom;
		paint = new Paint();
		paint.setColor(color);
		paint.setStrokeWidth(10);
	}
	
	/**
	 * android下所有的view控件的显示其实都是通过onDraw()
	 * canvas 代表的是屏幕的画布...
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		
		//bottom值 需要根据锅炉的压力 动态确定
		canvas.drawRect(20, 20,30,bottom,paint);
		super.onDraw(canvas);
	}

	
}


2、MainActivity

package com.njupt.monitor;

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.TextView;

public class MainActivity extends Activity {

	public native int getPressure();
	private Timer timer;
	private TimerTask task;
	private Handler handler = new Handler(){//消息机制的模板代码。。。在主线程中更新界面
		public void handleMessage(android.os.Message msg) {
			int pressure = (Integer) msg.obj;
			int color = getColor(pressure);
			if(color == 404){
				TextView tv = new TextView(MainActivity.this);
				tv.setTextColor(Color.RED);
				tv.setTextSize(30);
				tv.setText("锅炉快爆炸了...快跑吧~~~~~");
				
				
				setContentView(tv);
				timer.cancel();
				
				return ;
			}
			
			
			MyView myView = new MyView(MainActivity.this, pressure, color);
			setContentView(myView);//****这里需要注意,这时不再是通过.xml文件来画界面
			super.handleMessage(msg);
		};
	};
	
	
	static{
		System.loadLibrary("Hello");
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//获取锅炉压力 ,根据压力显示不同的内容
		timer = new Timer();
		task = new TimerTask() {
			
			@Override
			public void run() {
				int pressure = getPressure()%300;
				System.out.println("压力: " + pressure);
				
				//把压力显示到UI界面上
				Message msg = new Message();
				msg.obj = pressure;
				handler.sendMessage(msg);
			}
		};
		
		timer.schedule(task, 1000,2000);
	}

	/**
	 * 根据锅炉压力,获取应该显示的颜色
	 * @param pressure
	 * @return
	 */
	public int getColor(int pressure){
		if(pressure < 100){
			return Color.GREEN;
		}else if(pressure < 200){
			return Color.YELLOW;
		}else if(pressure < 260){
			return Color.RED;
		}else{
			return 404;
		}
	}
	@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;
	}

}


3、Hello.c

#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
#include "com_njupt_monitor_MainActivity.h"

#include <android/log.h>//include  D:\android-ndk-r7b\platforms\android-8\arch-arm\usr\include\android下的log.h这个目录
#define LOG_TAG "System.out"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)

/**
 * getpressure()的代码由C/C++工程师提供
 */
int getpressure(){
  // c语言中的随机数
    return rand();
}

JNIEXPORT jint JNICALL Java_com_njupt_monitor_MainActivity_getPressure
  (JNIEnv * env, jobject obj){
	return getpressure();
}




4、Android.mk

 LOCAL_PATH := $(call my-dir)

   include $(CLEAR_VARS)

   LOCAL_MODULE    := Hello   
   LOCAL_SRC_FILES := Hello.c
   LOCAL_LDLIBS += -llog
   
   include $(BUILD_SHARED_LIBRARY)


5、在此过程中需要用到的命令请参考上一篇博客。。。。




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值