JNI开发之锅炉压力监控器

来源:互联网 发布:全国通软件坑人 编辑:程序博客网 时间:2024/04/20 09:42

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

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


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

一、原理图



二、效果图




三、代码实现

1、MyView

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.njupt.monitor;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Paint;  
  6. import android.view.View;  
  7.   
  8. public class MyView extends View {  
  9.   
  10.     private int bottom;  
  11.     private Paint paint;  
  12.       
  13.     public MyView(Context context,int bottom,int color) {  
  14.         super(context);  
  15.           
  16.         this.bottom = bottom;  
  17.         paint = new Paint();  
  18.         paint.setColor(color);  
  19.         paint.setStrokeWidth(10);  
  20.     }  
  21.       
  22.     /** 
  23.      * android下所有的view控件的显示其实都是通过onDraw() 
  24.      * canvas 代表的是屏幕的画布... 
  25.      */  
  26.     @Override  
  27.     protected void onDraw(Canvas canvas) {  
  28.           
  29.         //bottom值 需要根据锅炉的压力 动态确定  
  30.         canvas.drawRect(2020,30,bottom,paint);  
  31.         super.onDraw(canvas);  
  32.     }  
  33.   
  34.       
  35. }  


2、MainActivity

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.njupt.monitor;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.app.Activity;  
  10. import android.graphics.Color;  
  11. import android.view.Menu;  
  12. import android.widget.TextView;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     public native int getPressure();  
  17.     private Timer timer;  
  18.     private TimerTask task;  
  19.     private Handler handler = new Handler(){//消息机制的模板代码。。。在主线程中更新界面  
  20.         public void handleMessage(android.os.Message msg) {  
  21.             int pressure = (Integer) msg.obj;  
  22.             int color = getColor(pressure);  
  23.             if(color == 404){  
  24.                 TextView tv = new TextView(MainActivity.this);  
  25.                 tv.setTextColor(Color.RED);  
  26.                 tv.setTextSize(30);  
  27.                 tv.setText("锅炉快爆炸了...快跑吧~~~~~");  
  28.                   
  29.                   
  30.                 setContentView(tv);  
  31.                 timer.cancel();  
  32.                   
  33.                 return ;  
  34.             }  
  35.               
  36.               
  37.             MyView myView = new MyView(MainActivity.this, pressure, color);  
  38.             setContentView(myView);//****这里需要注意,这时不再是通过.xml文件来画界面  
  39.             super.handleMessage(msg);  
  40.         };  
  41.     };  
  42.       
  43.       
  44.     static{  
  45.         System.loadLibrary("Hello");  
  46.     }  
  47.       
  48.       
  49.     @Override  
  50.     protected void onCreate(Bundle savedInstanceState) {  
  51.         super.onCreate(savedInstanceState);  
  52.         //获取锅炉压力 ,根据压力显示不同的内容  
  53.         timer = new Timer();  
  54.         task = new TimerTask() {  
  55.               
  56.             @Override  
  57.             public void run() {  
  58.                 int pressure = getPressure()%300;  
  59.                 System.out.println("压力: " + pressure);  
  60.                   
  61.                 //把压力显示到UI界面上  
  62.                 Message msg = new Message();  
  63.                 msg.obj = pressure;  
  64.                 handler.sendMessage(msg);  
  65.             }  
  66.         };  
  67.           
  68.         timer.schedule(task, 1000,2000);  
  69.     }  
  70.   
  71.     /**  
  72.      * 根据锅炉压力,获取应该显示的颜色  
  73.      * @param pressure  
  74.      * @return  
  75.      */  
  76.     public int getColor(int pressure){  
  77.         if(pressure < 100){  
  78.             return Color.GREEN;  
  79.         }else if(pressure < 200){  
  80.             return Color.YELLOW;  
  81.         }else if(pressure < 260){  
  82.             return Color.RED;  
  83.         }else{  
  84.             return 404;  
  85.         }  
  86.     }  
  87.     @Override  
  88.     public boolean onCreateOptionsMenu(Menu menu) {  
  89.         // Inflate the menu; this adds items to the action bar if it is present.  
  90.         getMenuInflater().inflate(R.menu.main, menu);  
  91.         return true;  
  92.     }  
  93.   
  94. }  


3、Hello.c

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <jni.h>  
  3. #include <stdlib.h>  
  4. #include "com_njupt_monitor_MainActivity.h"  
  5.   
  6. #include <android/log.h>//include  D:\android-ndk-r7b\platforms\android-8\arch-arm\usr\include\android下的log.h这个目录  
  7. #define LOG_TAG "System.out"  
  8. #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)  
  9. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)  
  10.   
  11. /** 
  12.  * getpressure()的代码由C/C++工程师提供 
  13.  */  
  14. int getpressure(){  
  15.   // c语言中的随机数  
  16.     return rand();  
  17. }  
  18.   
  19. JNIEXPORT jint JNICALL Java_com_njupt_monitor_MainActivity_getPressure  
  20.   (JNIEnv * env, jobject obj){  
  21.     return getpressure();  
  22. }  


4、Android.mk

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3.   include $(CLEAR_VARS)  
  4.   
  5.   LOCAL_MODULE    := Hello     
  6.   LOCAL_SRC_FILES := Hello.c  
  7.   LOCAL_LDLIBS += -llog  
  8.     
  9.   include $(BUILD_SHARED_LIBRARY)  


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


0 0
原创粉丝点击