欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

JNI开发之锅炉压力监控器

发布时间:2025/6/15 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JNI开发之锅炉压力监控器 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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


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

一、原理图



二、效果图




三、代码实现

1、MyView

[java] view plaincopyprint?
  • 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(2020,30,bottom,paint);  
  •         super.onDraw(canvas);  
  •     }  
  •   
  •       
  • }  


  • 2、MainActivity

    [java] view plaincopyprint?
  • 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

    [cpp] view plaincopyprint?
  • #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

    [cpp] view plaincopyprint?
  • LOCAL_PATH := $(call my-dir)  
  •   
  •   include $(CLEAR_VARS)  
  •   
  •   LOCAL_MODULE    := Hello     
  •   LOCAL_SRC_FILES := Hello.c  
  •   LOCAL_LDLIBS += -llog  
  •     
  •   include $(BUILD_SHARED_LIBRARY)  


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


    总结

    以上是生活随笔为你收集整理的JNI开发之锅炉压力监控器的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。