基于Android的城市环境监测系统

可借用代码部分:

1.页面跳转功能

基于Android的城市环境监测系统_第1张图片

 添加要跳转的页面java名。基于Android的城市环境监测系统_第2张图片

 建一个要跳转的页面的java文件

基于Android的城市环境监测系统_第3张图片

写一个与之对应的layout基于Android的城市环境监测系统_第4张图片

 主文件中先写入触发绑定的事件,如按到什么键。此程序是按到相应的菜单后跳转,

随后输入代码

 Intent intent1 = new Intent();
                intent1.setClass(MainActivity.this, Baojing.class);
                startActivity(intent1);
                Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();
             

Intent进行绑定,Baojing换成你要跳转的页面。

跳转页面的java文件写入该代码

基于Android的城市环境监测系统_第5张图片

 2.跳出弹窗消息代码

 Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();

"可自己加"如果要在加上参数则可仿造下面这样

 Toast.makeText(MainActivity.this,"最高湿度是:"+max2,Toast.LENGTH_LONG).show();

3.右上角菜单显示

第一步先定义

 static final int MENU_01 = Menu.FIRST;
    static final int MENU_02 = Menu.FIRST+1;
    static final int MENU_03 = Menu.FIRST+2;
    static final int MENU_04 = Menu.FIRST+3;

    static final int SUB_MENU_A = Menu.FIRST+4;
    static final int SUB_MENU_B = Menu.FIRST+5;
    static final int SUB_MENU_C = Menu.FIRST+6;
    static final int SUB_MENU_D = Menu.FIRST+7;
 

前面4个为一级菜单,后面A,B,C为2级菜单,

写入代码

    public boolean onCreateOptionsMenu(Menu menu){
        SubMenu uploadMenu=(SubMenu)menu.addSubMenu(0,MENU_01,1,"城市空气");
        uploadMenu.setHeaderTitle("空气系统设置");
        uploadMenu.add(0, SUB_MENU_A, 0, "PM2.5");
        uploadMenu.add(0, SUB_MENU_B, 0, "甲醛");
        SubMenu menu2 = (SubMenu)menu.addSubMenu(0,MENU_02,1,"城市温度");
        menu2.setHeaderTitle("温度系统设置");
        menu2.add(0, SUB_MENU_C, 0, "平均温度");
   
        return true;
    }
    @Override
 写完代码他只是空有其表,在加入代码,让他可以有对应的操作,

先在开头定义

TextView tv1;

之后

public boolean onOptionsItemSelected(MenuItem item){
        tv1 = (TextView) findViewById(R.id.info);
        switch (item.getItemId())
        {
            case SUB_MENU_A:
                Intent intent4 = new Intent();
                intent4.setClass(MainActivity.this, AirActivity.class);
                startActivity(intent4);
                return true;

            case SUB_MENU_B:
                return true;
            case SUB_MENU_C:
                int sum1 = 0;
                for (int i = 0;i<7;i++){
                    sum1 = sum1+temperatureArray[i];
                }
                Toast.makeText(MainActivity.this,"平均温度是:"+sum1/7,Toast.LENGTH_LONG).show();
                tv1.setText("平均温度是:"+sum1/7);
                return true;
            case SUB_MENU_D:
                int min1 = temperatureArray[0];
                for (int i = 1;i<7;i++){
                    if (min1>temperatureArray[i])
                        min1 = temperatureArray[i];
                }
                Toast.makeText(MainActivity.this,"最低温度是:"+min1,Toast.LENGTH_LONG).show();
                tv1.setText("最低温度是:" + min1);
                return true;
            case SUB_MENU_E:
                int max1 = temperatureArray[0];
                for (int i = 1;i<7;i++){
                    if (max130)
                    Toast.makeText(MainActivity.this,"高温警报!!",Toast.LENGTH_LONG).show();
                return true;
            case SUB_MENU_F:
                float sum2 = 0;
                for (int i = 0;i<7;i++){
                    sum2 = sum2+humidityArray[i];
                }
                Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
                tv1.setText("平均湿度是:"+sum2/7);
                return true;
            case SUB_MENU_G:
                float min2 = humidityArray[0];
                for (int i = 1;i<7;i++){
                    if (min2>humidityArray[i])
                        min2 = humidityArray[i];
                }
                Toast.makeText(MainActivity.this,"最低湿度是:"+min2,Toast.LENGTH_LONG).show();
                tv1.setText("最低湿度是:"+min2);
                return true;
            case SUB_MENU_H:
                float max2 = humidityArray[0];
                for (int i = 1;i<7;i++){
                    if (max2

 来加入内容,每个菜单都有不同的功能。

4.单片机传到安卓软件的数据存储以及数组的使用与遍历

基于Android的城市环境监测系统_第6张图片

 前面有了数据的java文件后,我们需要在主函数里面定义

    static final int temperatureArray[] = new int[7];
    static final float humidityArray[] = new float[7];

在第一个函数的里面写入该代码

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        String[] s1 = DateGenerater.getTemperatures().split(",");
        for(int i = 0;i < 7;i++){
            temperatureArray[i] = Integer.valueOf(s1[i].trim());
        }

        String[] s2 = DateGenerater.getHumiditys().split(",");
        for (int i = 0;i < 7;i++){
            humidityArray[i]= Float.valueOf(s2[i].trim());
        }
    }

意思是把DateGenerater.getTemperatures的这个数据变成String类型,split(“,”)是为了把逗号去掉,因为我们的java函数里面有逗号,需要运用函数去除,之后采用for循环进行遍历,trim是移除字符串两侧的空白字符或其他预定义字符。

Float.valueOf(s2[i].trim());意思是把s2【i】变量变成Float类型。这样  humidityArray[i]最终得到的是float类型的值。

前面的步骤完成后写入代码

 case SUB_MENU_F:
                float sum2 = 0;
                for (int i = 0;i<7;i++){
                    sum2 = sum2+humidityArray[i];
                }
                Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
                tv1.setText("平均湿度是:"+sum2/7);
                return true;

效果图:

基于Android的城市环境监测系统_第7张图片

 

5.tv的显示

tv.setText("text");

视频展示:

基于Android的城市环境监测系统

全部代码

MainActivity:

package com.huangfushi.hfs192025214_shuzhicaidan2;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    static final int MENU_01 = Menu.FIRST;
    static final int MENU_02 = Menu.FIRST+1;
    static final int MENU_03 = Menu.FIRST+2;
    static final int MENU_04 = Menu.FIRST+3;

    static final int SUB_MENU_A = Menu.FIRST+4;
    static final int SUB_MENU_B = Menu.FIRST+5;
    static final int SUB_MENU_C = Menu.FIRST+6;
    static final int SUB_MENU_D = Menu.FIRST+7;
    static final int SUB_MENU_E = Menu.FIRST+8;
    static final int SUB_MENU_F = Menu.FIRST+9;
    static final int SUB_MENU_G = Menu.FIRST+10;
    static final int SUB_MENU_H = Menu.FIRST+11;
    static final int SUB_MENU_I = Menu.FIRST+12;


    static final int temperatureArray[] = new int[7];
    static final float humidityArray[] = new float[7];
    TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        String[] s1 = DateGenerater.getTemperatures().split(",");
        for(int i = 0;i < 7;i++){
            temperatureArray[i] = Integer.valueOf(s1[i].trim());
        }

        String[] s2 = DateGenerater.getHumiditys().split(",");
        for (int i = 0;i < 7;i++){
            humidityArray[i]= Float.valueOf(s2[i].trim());
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        SubMenu uploadMenu=(SubMenu)menu.addSubMenu(0,MENU_01,1,"城市空气");
        uploadMenu.setHeaderTitle("空气系统设置");
        uploadMenu.add(0, SUB_MENU_A, 0, "PM2.5");
        uploadMenu.add(0, SUB_MENU_B, 0, "甲醛");
        SubMenu menu2 = (SubMenu)menu.addSubMenu(0,MENU_02,1,"城市温度");
        menu2.setHeaderTitle("温度系统设置");
        menu2.add(0, SUB_MENU_C, 0, "平均温度");
        menu2.add(0, SUB_MENU_D, 0, "最低温度");
        menu2.add(0, SUB_MENU_E, 0, "最高温度");
        SubMenu menu3 = (SubMenu)menu.addSubMenu(0,MENU_03,1,"城市湿度");
        menu3.setHeaderTitle("湿度系统设置");
        menu3.add(0, SUB_MENU_F, 0, "平均湿度");
        menu3.add(0,SUB_MENU_G,0,"最低湿度");
        menu3.add(0, SUB_MENU_H, 0, "最高湿度");
        SubMenu menu4 = (SubMenu)menu.addSubMenu(0,MENU_04,1,"报警设置");
        menu4.setHeaderTitle("报警系统设置");
        menu4.add(0, SUB_MENU_I, 0, "参数阀值设置");
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        tv1 = (TextView) findViewById(R.id.info);
        switch (item.getItemId())
        {
            case SUB_MENU_A:
                Intent intent4 = new Intent();
                intent4.setClass(MainActivity.this, AirActivity.class);
                startActivity(intent4);
                return true;

            case SUB_MENU_B:
                return true;
            case SUB_MENU_C:
                int sum1 = 0;
                for (int i = 0;i<7;i++){
                    sum1 = sum1+temperatureArray[i];
                }
                Toast.makeText(MainActivity.this,"平均温度是:"+sum1/7,Toast.LENGTH_LONG).show();
                tv1.setText("平均温度是:"+sum1/7);
                return true;
            case SUB_MENU_D:
                int min1 = temperatureArray[0];
                for (int i = 1;i<7;i++){
                    if (min1>temperatureArray[i])
                        min1 = temperatureArray[i];
                }
                Toast.makeText(MainActivity.this,"最低温度是:"+min1,Toast.LENGTH_LONG).show();
                tv1.setText("最低温度是:" + min1);
                return true;
            case SUB_MENU_E:
                int max1 = temperatureArray[0];
                for (int i = 1;i<7;i++){
                    if (max130)
                    Toast.makeText(MainActivity.this,"高温警报!!",Toast.LENGTH_LONG).show();
                return true;
            case SUB_MENU_F:
                float sum2 = 0;
                for (int i = 0;i<7;i++){
                    sum2 = sum2+humidityArray[i];
                }
                Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
                tv1.setText("平均湿度是:"+sum2/7);
                return true;
            case SUB_MENU_G:
                float min2 = humidityArray[0];
                for (int i = 1;i<7;i++){
                    if (min2>humidityArray[i])
                        min2 = humidityArray[i];
                }
                Toast.makeText(MainActivity.this,"最低湿度是:"+min2,Toast.LENGTH_LONG).show();
                tv1.setText("最低湿度是:"+min2);
                return true;
            case SUB_MENU_H:
                float max2 = humidityArray[0];
                for (int i = 1;i<7;i++){
                    if (max2
DateGenerater:
package com.huangfushi.hfs192025214_shuzhicaidan2;

public class DateGenerater {


    public static String getpm(){
        return "21,80,160";
    }

    public static String getTemperatures(){
        return "21,22,19,28,29,32,35";
    }

    public static String getHumiditys() {
        return "0.45, 0.23,0.58,0.32,0.44,0.56,0.48";
    }

    public static int gaowen=10;
}
Baojing;
package com.huangfushi.hfs192025214_shuzhicaidan2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class Baojing  extends MainActivity {
    @Override

    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.baojing);
        Button btnButton = (Button) findViewById(R.id.btnBack);
        btnButton.setOnClickListener(listener);//为Button设置监听事件
    }

    private View.OnClickListener listener = new View.OnClickListener() {//创建监听对象
        @Override
        public void onClick(View v) {

            Intent intent2 = new Intent();// 创建Intent对象

            setResult(1, intent2);//设置返回标识
            finish();//关闭Activity
        }
    };
}
AirActivity:
package com.huangfushi.hfs192025214_shuzhicaidan2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;


public class AirActivity extends Activity {
    private TextView t1;
    private TextView t2;
    private TextView t3;
    static final int pmArray[] = new int[3];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.air);

        String[] s3 = DateGenerater.getpm().split(",");
        for(int i = 0;i < 3;i++){
            pmArray[i] = Integer.valueOf(s3[i].trim());
        }
        t1 = (TextView) findViewById(R.id.air_tv1);
        t2 = (TextView) findViewById(R.id.air_tv2);
        t3 = (TextView) findViewById(R.id.air_tv3);
        setValue(t1, pmArray[0]);
        setValue(t2, pmArray[1]);
        setValue(t3, pmArray[2]);
        int sum1 = 0;

        sum1 = pmArray[1];

        if (sum1<50)
        Toast.makeText(AirActivity.this,"经检测PM2.5,空气质量优",Toast.LENGTH_LONG).show();
        if(sum1>50&sum1<100)
            Toast.makeText(AirActivity.this,"经检测PM2.5,空气质量良",Toast.LENGTH_LONG).show();
            if(sum1>100&sum1<150)
                Toast.makeText(AirActivity.this,"经检测PM2.5,达到轻度污染",Toast.LENGTH_LONG).show();
            if(sum1>150)
                Toast.makeText(AirActivity.this,"经检测PM2.5,达到重度污染",Toast.LENGTH_LONG).show();
    }


private void setValue(TextView textView, int pm) {
        textView. setText(pm+"ppm");


        }

        }

layout部分:




    
    


air.xm



    
        
            
            
            

        
        
            
            
            
            
            
            
        
    

baojing.xml:




    
    
    
    
    
    
    

主头文件写该代码。




    
        
            
                

                
            
        
        
        
        
        
    


完整工程布局:

基于Android的城市环境监测系统_第8张图片

 

你可能感兴趣的:(Android,studio,java,android-studio,c语言,android)