NDK开发综合案例——锅炉压力显示系统

Demo下载

https://github.com/linweimao/GuoLuApplication/tree/master

锅炉压力系统

  1. 创建一个项目,名字叫GuoLu

    1. 写 JNI.java(或者:在MainActivity里面写,得到锅炉压力值的native方法 getPressure()

    2. 在 Android.mk 文件中配置生成 .so文件的名称

      Android.mk

      LOCAL_PATH := $(call my-dir)
      include $(CLEAR_VARS)
      
      LOCAL_LDLIBS := -llog
      LOCAL_MODULE := guolu
      LOCAL_SRC_FILES := GuoLu.c
      
      include $(BUILD_SHARED_LIBRARY)
      

      Application.mk

      APP_ABI := all
      APP_STL := stlport_static
      
  2. 分析实现的原理

  3. 在C代码中写锅炉压力值,返回给Java

    int getPressure(){
    	int pressure = rand()%250;
    	return pressure;
    } 
    

    在 C 代码中实现如下:

    //
    // Created by 林伟茂 on 2020/4/12.
    //
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <jni.h>
    
    
    /*
     * 得到锅炉的压力值
     * 0~250之间
    */
    
    int getPressure(){
       int pressure = rand()%250;
       return pressure;
    }
    
    /*
     * 从锅炉感应器中得到锅炉压力值
    */
    jint  Java_com_lwm_guoluapplication_MainActivity_getPressure(JNIEnv *env, jobject instance) {
        int pressur = getPressure();
        return pressur;
    }
    
  4. 在视图中动态绘制

1.锅炉显示压力结构体

2.测试模拟锅炉压力值C测试代码

#include<stdio.h>
#include<stdlib.h>
/**
模拟-得到锅炉的压力值范围:0~250  
*/

int pressure = 50; //当前压力值,初始为50  
//得到当前 
int getPressure(){
	int increase = rand()%20; 
	pressure += increase;
   return pressure;   
}     

main(){
	while(1){       
	       sleep(500);
	       int pressure = getPressure();
	       if(pressure<=200) {
	       		printf("压力值为%d, 在正常范围内 \n",pressure); 
		   } else if(pressure<=250) {
		   		printf("压力值为%d, 压力偏高 \n",pressure); 
		   } else {
		   		printf("压力值超过了最大值, 要爆炸啦! \n"); 
		   } 
	}            
}

3. native方法

//native代码-得到锅炉的压力值

public native int getPressure();

4. C函数

#include <jni.h>

#include <stdlib.h>

int pressure = 50; //当前压力值,初始为50

//得到当前

int getPressure(){

	int increase = rand()%20;

	pressure += increase;

    return pressure;

}

JNIEXPORT jint JNICALL Java_com_atguigu_guolu_MainActivity_getPressure

  (JNIEnv * env, jobject jobj) {

	return getPressure();
}

5. 自定义View

public class PressureView extends View {

	private int max = 250;
	private int pressure;
	private Paint paint;
	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getPressure() {
		return pressure;
	}
	public void setPressure(int pressure) {
		this.pressure = pressure;
		postInvalidate();
	}
	public PressureView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();

		paint.setTextSize(20);

		paint.setAntiAlias(true);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		if(pressure>max) {
			paint.setColor(Color.RED);
			canvas.drawText("要爆炸了, 兄弟快跑!", 0, getHeight()/2, paint);
		} else {
			paint.setColor(Color.GRAY);

			canvas.drawRect(20, 0, 60, getHeight(), paint);

			int p = pressure*250/max;
			if(p<200) {
				paint.setColor(Color.GREEN);
			} else {
				paint.setColor(Color.RED);
			}
			canvas.drawRect(20, getHeight()-getHeight()*pressure/max, 60, getHeight(), paint);
		}
	}
}

6. 调用C更新界面

new Thread(new Runnable() {
	@Override
	public void run() {
		while(true) {
			SystemClock.sleep(500);
			int pressure = getPressure();
			pv_main.setPressure(pressure);
			if(pressure>250) {
				return;
			}
		}
	}
}).start();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于您的问题,我会尽力回答。在Android中,JNI可以用来与C/C++代码进行交互,我们可以利用JNI来读写SD卡。下面是一个简单的例子: 1. 首先,在您的Java代码中定义native方法: ``` public class MainActivity extends Activity { static { System.loadLibrary("native-lib"); } public native String readFromSDCard(String filePath); public native boolean writeToSDCard(String filePath, String content); } ``` 2. 然后,在您的C/C++代码中实现这些方法: ``` #include <jni.h> #include <string.h> #include <stdio.h> #include <android/log.h> JNIEXPORT jstring JNICALL Java_com_example_myapplication_MainActivity_readFromSDCard(JNIEnv *env, jobject instance, jstring filePath) { const char *path = env->GetStringUTFChars(filePath, 0); FILE *file = fopen(path, "r"); if (file == NULL) { __android_log_print(ANDROID_LOG_ERROR, "JNI", "readFromSDCard: fopen failed"); return NULL; } fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); char *buffer = new char[size + 1]; fread(buffer, 1, size, file); buffer[size] = '\0'; fclose(file); env->ReleaseStringUTFChars(filePath, path); return env->NewStringUTF(buffer); } JNIEXPORT jboolean JNICALL Java_com_example_myapplication_MainActivity_writeToSDCard(JNIEnv *env, jobject instance, jstring filePath, jstring content) { const char *path = env->GetStringUTFChars(filePath, 0); const char *str = env->GetStringUTFChars(content, 0); FILE *file = fopen(path, "w"); if (file == NULL) { __android_log_print(ANDROID_LOG_ERROR, "JNI", "writeToSDCard: fopen failed"); return false; } fwrite(str, 1, strlen(str), file); fclose(file); env->ReleaseStringUTFChars(filePath, path); env->ReleaseStringUTFChars(content, str); return true; } ``` 3. 最后,在您的AndroidManifest.xml文件中添加以下权限: ``` <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 这样,您就可以在Java代码中调用这些native方法来读写SD卡了。 至于Android NDK的使用实例——增量更新实战,这是一个比较复杂的话题,如果您有相关的需求,可以提出具体的问题,我会尽力为您解答。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值