【JNI】锅炉压力监测器

public class MainActivity extends Activity {

    private MyProgressBar pb;

    static{
        System.loadLibrary("hello");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (MyProgressBar) findViewById(R.id.pb);
        pb.setMax(100);
    }

   public void start(View v){
       new Thread(){
           public void run() {
               startMonitor();
           };
       }.start();
   }
   public void stop(View v){
       stopMonitor();
   }

   public native void startMonitor();
   public native void stopMonitor();

   public void show(int pressure){
       pb.setPressure(pressure);
   }
}
public class MyProgressBar extends View {

    private int pressure;
    private int max;
    public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyProgressBar(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void setPressure(int pressure) {
        this.pressure = pressure;
        //使当前视图无效,如果当前视图是可见的,那么onDraw会调用,只能在主线程使用
//        invalidate();
        postInvalidate();
    }
    public void setMax(int max) {
        this.max = max;
    }
    @Override
    protected void onDraw(Canvas canvas) {

        Paint paint = new Paint();

        if(pressure < 50){
            paint.setColor(Color.GREEN);
        }
        else if(pressure < 80){
            paint.setColor(0xffff8800);
        }
        else {
            paint.setColor(Color.RED);

        }

        canvas.drawRect(10, 10 + max - pressure, 30, 10 + max, paint);
    }
}
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>

int getPressure(){
    //……监测电压……运算得到压力值
    ;
}

int monitor;
JNIEXPORT void JNICALL Java_com_itheima_monitor_MainActivity_startMonitor
  (JNIEnv * env, jobject obj){
    monitor = ;
    while(monitor){
        //获取锅炉压力
        int pressure = getPressure();

        //jclass      (*FindClass)(JNIEnv*, const char*);
        jclass clazz = (*env)->FindClass(env, "com/itheima/monitor/MainActivity");
        //jmethodID   (*GetMethodID)(JNIEnv*, jclass, const char*, const char*);
        jmethodID methodId = (*env)->GetMethodID(env, clazz, "show", "(I)V");
        //void        (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...);
        (*env)->CallVoidMethod(env, obj, methodId, pressure);
        sleep();
    }

}

JNIEXPORT void JNICALL Java_com_itheima_monitor_MainActivity_stopMonitor
 (JNIEnv * env, jobject obj){
    monitor = ;
}

Application.mk

APP_ABI := armeabi x86

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello
LOCAL_SRC_FILES := hello.c

include $(BUILD_SHARED_LIBRARY)
上一篇:LVM 管理减少swap分区空间增加到根分区


下一篇:【泛化物品】【HDU1712】【ACboy needs your help】