Android 下 JNI 开发

7.设置动态的压力值

 

new Thread(){

public void run() {

while (true) {

SystemClock.sleep(1000);

Random random = new Random();

int pressure= random.nextInt() % 210;

myPressureView.setPressure(pressure);

}

};

}.start();

 

 

8.需要在setPressure方法中设置重画方法

 

    //重画

//invalidate();只能在主线程中调用

//postInvalidate();可以在子线程中调用

postInvalidate();

 

9.解决图形向下移动bug,取绝对值

new Thread(){

public void run() {

while (true) {

SystemClock.sleep(1000);

Random random = new Random();

int pressure= random.nextInt() % 210;

myPressureView.setPressure(Math.abs(pressure));

}

};

}.start();

 

 

 

10.锅炉爆炸后停止工作

new Thread(){

public void run() {

while (true) {

SystemClock.sleep(1000);

Random random = new Random();

int pressure= Math.abs(random.nextInt() % 210);

myPressureView.setPressure(pressure);

if(pressure >200){

break;

}

}

};

}.start();

 

 

 

实现C语言端获取锅炉的压力值

  1.native代码

    /**

 * 从C端获取一个压力值

 * @return

 */

public native int getPressure();

 

   {

java.lang.System.loadLibrary("PressureDemo");

 }

 

 

   new Thread(){

public void run() {

while (true) {

SystemClock.sleep(1000);

// Random random = new Random();

int pressure= getPressure();//Math.abs(random.nextInt() % 250);

myPressureView.setPressure(pressure);

if(pressure >200){

break;

}

}

};

}.start();

 

 

 

C代码

 

#include "com_atguigu_pressuredemo_MainActivity.h"

#include<stdio.h>

#include<stdlib.h>

 

/**模拟-得到锅炉的压力值

范围:0~250

*/

int getPressure()

{

   return rand()%250;

 

}

 

 

JNIEXPORT jint JNICALL Java_com_atguigu_pressuredemo_MainActivity_getPressure

  (JNIEnv *env, jobject obj){

 

return getPressure();

}

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。