//运行代码
package com.example.deleteaiding;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.TypedValue;
import android.view.Gravity;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.location.LocationManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.OnNmeaMessageListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.os.Handler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    TableLayout tableLayout;
    private ArrayList<DataItem> dataItems = new ArrayList<DataItem>();
    private ArrayList<DataItem> dataItems_appear = new ArrayList<DataItem>();
    private TextView longitudeTextview;
    private TextView latitudeTextview;
    private LocationManager locationManager;
    private LocationListener locationListener;
    private OnNmeaMessageListener onNmeaMessageListener;
    private TextView stateTextview;
    private TextView PDOPTextview;//定位精度因子
    private TextView HDOPTextview;//水平精度因子
    private TextView VDOPTextview;//垂直精度因子
    private TextView altitudeTextview;//海拔
    private TextView speedTextview;
    private TextView GPS_satelliteTextview;
    private TextView GALILEO_satelliteTextview;
    private TextView GLONASS_satelliteTextview;
    private TextView BeiDou_satelliteTextview;
    private static final String TAG = "MainActivity";
    private static final String TAG1 = "NmeaDataReceiver";
    private LocationManager mLocationManager;
    private Button aid_location_button;
    private Button location_button;
    private Button save_nmea_data;
    private Switch locationlistener_switch;
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 100;
    private static final int REQUEST_WRITE_STORAGE = 112;
    private TextView secondsTextView;
    private ProgressBar progressBar5;
    public String nmea_data = "";
    public String getGsv_nmea_data_string1="";
    public String getGsv_nmea_data_string2="";
    public String GSA_nmea_data_string="";
    public String GSA_nmea_data_string1="";
    public String[] GGA_string_parts;
    public String[] VTG_string_parts;
    private boolean isRunning = false;//计时启动标志
    private boolean gsv_isRunning=false;//gsv数据显示标志
    private Handler handler = new Handler();
    String[] GPS_signal_channel = {"All Signals", "L1C/A", "L1P(Y)", "L1M", "L2P(Y)", "L2C-M", "L2C-L", "L5-I", "L5-Q"};
    String[] GLONASS_signal_channel = {"All Signals", "G1C-A", "G1P", "G2C/A", "G2P","","","","","","","","","","",""};
    String[] GALILEO_signal_channel = {"All Signals", "E5a", "E5b", "E5a+b", "E6-A", "E6-BC", "L1-A", "L1-BC","","","","",""};
    String[] BeiDou_signal_channel={"All Signals","B1I","B1Q","B1C","B1A","B2-a","B2-b","B2a+b","B3I","B3Q","B3A","B2I","B2Q","","","","",""};
    String[] QZSS_signal_channel={"All Signals","L1C/A","L1C(D)","L1C(P)","LIS","L2C-M","L2C-L","L5-I","L5-Q","L6D","L6E","","","","","",""};
    String[] NavIC_signal_channel={"All Signals","L5-SPS","S-SPS","L5-RS","S-RS","L1-SPS","","","","","","","",""};
    String[] location_state = {"", "未定位", "2D定位", "3D定位"};
    String[] system = {"", "GPS", "GLONASS", "GALILEO", "BeiDou"};

    // 记录秒表开始时间(单位:毫秒)
    private long startTime = 0;
    // 记录已经流逝的时间(单位:毫秒)
    private long elapsedTime = 0;

    private TextView system1 ;
    private TextView ID1 ;
    private TextView elevation1;
    private TextView azimuth1;
    private TextView SNR1;
    private TextView signal_channel1 ;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        secondsTextView = findViewById(R.id.seconds_text_view);
        longitudeTextview = findViewById(R.id.longitudeTextview);
        latitudeTextview = findViewById(R.id.latitudeTextview);
        stateTextview = findViewById(R.id.stateTextview);
        PDOPTextview = findViewById(R.id.PDOPTextview);
        HDOPTextview = findViewById(R.id.HDOPTextview);
        VDOPTextview = findViewById(R.id.VDOPTextview);
        GPS_satelliteTextview = findViewById(R.id.GPS_satelliteTextview);
        GALILEO_satelliteTextview = findViewById(R.id.GALILEO_satelliteTextview);
        GLONASS_satelliteTextview = findViewById(R.id.GLONASS_satelliteTextview);
        BeiDou_satelliteTextview = findViewById(R.id.BeiDou_satelliteTextview);
        altitudeTextview = findViewById(R.id.altitudeTextview);
        speedTextview=findViewById(R.id.speedTextview);
        system1=findViewById(R.id.system1);
        ID1=findViewById(R.id.ID1);
        elevation1=findViewById(R.id.elevation1);
        azimuth1=findViewById(R.id.azimuth1);
        SNR1=findViewById(R.id.SNR1);
        signal_channel1=findViewById(R.id.signal_channel1);


        // 初始化位置管理器
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        progressBar5 = findViewById(R.id.progressBar5);
        //initTable();

        //可见星更新

        //dataItems.clear();//清除表格
        //initTable();// 绘制表格
        // 检查权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_STORAGE);
        }
        //定位监听
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                // 当位置发生变化时,更新UI显示新的位置信息
                //updateLocationUI(location);
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
                Toast.makeText(MainActivity.this, "定位服务已关闭,请开启定位服务", Toast.LENGTH_SHORT).show();
            }
        };
        onNmeaMessageListener=new OnNmeaMessageListener() {
            @Override
            public void onNmeaMessage(String message, long timestamp) {
                if (message != null) {
                    message=message+"\n";
                    nmea_data=nmea_data+message;
                    if(nmea_data.length()>5000){
                        nmea_data="";
                    }
                    String[] nmea_parts = message.split(",");
                    if (message.substring(3, 6).equals("GSV")) {
                        getGsv_nmea_data_string1=getGsv_nmea_data_string1+message;
                    }

                    if(!message.substring(3, 6).equals("GSV")){
                        if(!Objects.equals(getGsv_nmea_data_string1, "")) {
                            getGsv_nmea_data_string2 = getGsv_nmea_data_string1;
                        }
                        getGsv_nmea_data_string1="";
                    }
                    if (nmea_parts[0].equals("$GNGSA")) {
                        GSA_nmea_data_string=GSA_nmea_data_string+message;
                    }
                    if(!Objects.equals(GSA_nmea_data_string, "")&&!nmea_parts[0].equals("$GNGSA")) {
                        GSA_nmea_data_string1 = GSA_nmea_data_string;
                        GSA_nmea_data_string = "";
                    }
                    if(nmea_parts[0].equals("$GNGGA")){
                        GGA_string_parts=nmea_parts;
                        isRunning=false;
                        progressBar5.setIndeterminate(false);
                    }
                    if(nmea_parts[0].equals("$GNVTG")){
                        VTG_string_parts=nmea_parts;
                    }
                }
            }
        };
        // 检查并请求定位权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    LOCATION_PERMISSION_REQUEST_CODE);
        }



        // 设置刷新按钮的点击事件
        aid_location_button = findViewById(R.id.aid_location_button);
        //  快速定位
        aid_location_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isRunning) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        if (location_service_judge()) {
                            boolean isMobileDataEnabled = isMobileDataOn();
                            if (isMobileDataEnabled) {
                                start_time_location();
                            } else {
                                Toast.makeText(MainActivity.this, "请打开手机移动网络", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "定位服务已关闭,请开启定位服务", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                LOCATION_PERMISSION_REQUEST_CODE);
                    }
                }
                else{
                    Toast.makeText(MainActivity.this, "正在定位中...", Toast.LENGTH_SHORT).show();
                }
            }
        });
        // 无辅助信息定位
        location_button = findViewById(R.id.location_button);
        // delete aiding data按钮点击事件
        location_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 检查定位权
                if(!isRunning) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        if (location_service_judge()) {
                            boolean isMobileDataEnabled = isMobileDataOn();
                            if (!isMobileDataEnabled) {
                                start_time_location();
                            } else {
                                Toast.makeText(MainActivity.this, "请关闭手机移动网络", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "定位服务已关闭,请开启定位服务", Toast.LENGTH_SHORT).show();
                        }
                        // 再执行sendColdCommand函数
                    } else {
                        // 请求定位权限
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                LOCATION_PERMISSION_REQUEST_CODE);
                    }
                }
                else{
                    Toast.makeText(MainActivity.this, "正在定位中...", Toast.LENGTH_SHORT).show();
                }

            }
        });
        save_nmea_data = findViewById(R.id.save_nmea_data);
        //保存nmea数据
        save_nmea_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                write_nmea_data();
            }
        });
       //位置监听按钮开关
        locationlistener_switch = findViewById(R.id.locationlistener_switch);
        locationlistener_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        if(location_service_judge()){
                            startLocationUpdates();
                        }
                        else{
                            locationlistener_switch.setChecked(false);
                            Toast.makeText(MainActivity.this, "定位服务已关闭,请开启定位服务", Toast.LENGTH_SHORT).show();
                        }

                    }
                    else{
                        locationlistener_switch.setChecked(false);
                        // 请求定位权限
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                LOCATION_PERMISSION_REQUEST_CODE);

                    }
                } else {
                    stop_location_listening();//删除所有显示,关闭位置监听
                }
            }
        });

    }

////////////  // // /////////////////函数部分//////////////////////////

    private void start_gsv_update(){
        Thread thread_gsv = new Thread(new Runnable() {
            @Override
            public void run() {
                while (gsv_isRunning) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            String system_string="";
                            String ID_string="";
                            String elevation_string="";
                            String azimuth_string="";
                            String SNR_string="";
                            String signal_channel_string="";
                            if(!Objects.equals(getGsv_nmea_data_string2, "")) {
                                String gsv_nmea_data_string = getGsv_nmea_data_string2;//从get_nmea_dataq获取,GSV结合起来的字符串
                                String[] gsv_nmea_data_string_parts = gsv_nmea_data_string.split("\n");
                                for (int j = 0; j < gsv_nmea_data_string_parts.length; j++) {
                                    if (!Objects.equals(gsv_nmea_data_string_parts[j], "")&&gsv_nmea_data_string_parts[j].substring(3,6).equals("GSV")) {
                                        String[] gsv_parts = gsv_nmea_data_string_parts[j].split(",");
                                        int long_parts = gsv_parts.length;
                                        int sum = (long_parts - 5) / 4;
                                        for (int i = 0; i < sum; i++) {
                                            system_string = system_string + "\n" + system_string_transformation(gsv_parts[0].substring(1, 3));
                                            ID_string = ID_string + "\n" + gsv_parts[i * 4 + 4];
                                            elevation_string = elevation_string + "\n" + gsv_parts[i * 4 + 5];
                                            azimuth_string = azimuth_string + "\n" + gsv_parts[i * 4 + 6];
                                            SNR_string = SNR_string + "\n" + gsv_parts[i * 4 + 7];
                                            signal_channel_string = signal_channel_string + "\n" + signal_channel_transformation(gsv_parts[0].substring(1, 3), gsv_parts[gsv_parts.length - 1].substring(0, 1));
                                        }
                                    }
                                }

                            }
                            String finalSystem_string = system_string;
                            String finalID_string = ID_string;
                            String finalElevation_string = elevation_string;
                            String finalAzimuth_string = azimuth_string;
                            String finalSNR_string = SNR_string;
                            String finalSignal_channel_string = signal_channel_string;

                            if(!finalSystem_string.isEmpty()) {
                                system1.setText(finalSystem_string);
                                ID1.setText(finalID_string);
                                elevation1.setText(finalElevation_string);
                                azimuth1.setText(finalAzimuth_string);
                                SNR1.setText(finalSNR_string);
                                signal_channel1.setText(finalSignal_channel_string);
                            }

                            if(!Objects.equals(GSA_nmea_data_string1, ""))
                            {
                                String GSA_nmea_data_string2=GSA_nmea_data_string1;//GSA字符串相加
                                String[] GSA_nmea_data_string2_parts=GSA_nmea_data_string2.split("\n");//将字符串拆成字符串数组
                                for(int i=0;i<GSA_nmea_data_string2_parts.length;i++){
                                    String[] GSA_string_parts1 = GSA_nmea_data_string2_parts[i].split(",");//GSA的每条数据用,分割
                                    if(GSA_string_parts1[0].equals("$GNGSA")) {
                                        int string_long_GSA = GSA_string_parts1.length;
                                        stateTextview.setText("定位状态:" + location_state[Integer.parseInt(GSA_string_parts1[2])]);//读取定位状态
                                        if (!GSA_string_parts1[2].equals("1")) {
                                            String satellite_string = "";

                                            for (int j = 3; j < 15; j++) {
                                                if (!GSA_string_parts1[j].isEmpty()) {
                                                    satellite_string = satellite_string + GSA_string_parts1[j] + " ";
                                                }
                                            }

                                            //satellite_string = satellite_string + GSA_string_parts1[14];//获得参与定位卫星ID号
                                            //将参与定位卫星ID号显示Integer.parseInt(nmea_parts[string_long - 1].substring(0, 1))
                                            location_satellite_appear(GSA_string_parts1[string_long_GSA - 1].substring(0, 1), satellite_string);
                                            //显示精度因子
                                            PDOPTextview.setText("位置精度因子:" + GSA_string_parts1[string_long_GSA - 2]);
                                            HDOPTextview.setText("水平精度因子:" + GSA_string_parts1[string_long_GSA - 3]);
                                            VDOPTextview.setText("垂直精度因子:" + GSA_string_parts1[string_long_GSA - 4]);
                                            if(GGA_string_parts!=null )
                                            {
                                                String[] GGA_string_parts1 = GGA_string_parts;
                                                latitudeTextview.setText("纬度:" + GGA_string_parts1[2].substring(0, 2) + "°" + GGA_string_parts1[2].substring(2) + "′" + GGA_string_parts1[3]);
                                                longitudeTextview.setText("经度:" + GGA_string_parts1[4].substring(0, 3) + "°" + GGA_string_parts1[4].substring(3) + "′" + GGA_string_parts1[5]);
                                                altitudeTextview.setText("海拔:" + GGA_string_parts1[9] + "m");
                                                //handler.removeCallbacks(runnable);
                                                //timerThread.interrupt();
                                                GGA_string_parts = null;
                                            }
                                        }
                                    }
                                }

                            }


                            if(VTG_string_parts!=null)
                            {
                                String[] VTG_string_parts1 = VTG_string_parts;
                                speedTextview.setText("速度:" + VTG_string_parts1[5] + "km/h");
                                VTG_string_parts = null;
                            }

                        }
                    });
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        thread_gsv.start();
    }
    private void start_time() {
        startTime = System.currentTimeMillis();
        //计时函数
        Thread timerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRunning) {
                    elapsedTime = System.currentTimeMillis() - startTime;
                    int a = (int) (elapsedTime / 100);
                    int seconds = (int) a / 10;
                    int seconds_10 = a % 10;
                    @SuppressLint("DefaultLocale") final String time = String.format("%d.%d s", seconds, seconds_10);
                    // 在主线程更新UI
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            secondsTextView.setText(time);
                        }
                    });
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        timerThread.start();
    }
    //删除星历函数
/*
    private boolean sendColdCommand() {
        Bundle bundleWarm = new Bundle();
        bundleWarm.putBoolean("ephemeris", true);
        boolean b = mLocationManager.sendExtraCommand(LocationManager.GPS_PROVIDER,
                "delete_aiding_data", bundleWarm);
        Log.i(TAG, "delete_aiding_data ephemeris " + b);
        return b;
    }
*/
    //删除所有参数
    private boolean sendColdCommand() {
        boolean b = mLocationManager.sendExtraCommand(LocationManager.GPS_PROVIDER,
                "delete_aiding_data", null);
        Log.i(TAG, "delete_aiding_data null" + b);
        return b;
    }
    //删除成功提示
    private void showSuccessToast() {
        Toast.makeText(this, "辅助信息删除成功", Toast.LENGTH_SHORT).show();
    }

    //位置更新
    private void startLocationUpdates() {
        try {

            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            locationlistener_switch.setChecked(true);//位置监听开启
            progressBar5.setIndeterminate(true);//进度条开启
            mLocationManager.addNmeaListener(onNmeaMessageListener);
            //start_other_update();
            gsv_isRunning=true;//开始显示gsv
            start_gsv_update();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
    //检测定位服务是否开启
    private boolean location_service_judge() {
        //LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        return gpsEnabled;
    }

    private void stop_location_listening() {
        // 移除位置更新监听器,避免内存泄漏
        if (mLocationManager != null) {
            try {
                mLocationManager.removeUpdates(locationListener);
                mLocationManager.removeNmeaListener(onNmeaMessageListener);
                isRunning=false;//停止计时
                gsv_isRunning=false;//gsv显示停止
                progressBar5.setIndeterminate(false);//进度条停止
                //handler.removeCallbacks(runnable);//移除可见卫星更新
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        clear_textview();//移除所有显示
                        locationlistener_switch.setChecked(false);//将位置监听按钮设置为关
                    }
                }, 100);//延迟100ms执行
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
    }


    private void clear_textview(){
        stateTextview.setText("定位状态:");
        PDOPTextview.setText("位置精度因子:");
        HDOPTextview.setText("水平精度因子:" );
        VDOPTextview.setText("垂直精度因子:" );
        latitudeTextview.setText("纬度:" );
        longitudeTextview.setText("经度:");
        altitudeTextview.setText("海拔:");
        speedTextview.setText("速度:");
        GPS_satelliteTextview.setText("GPS:");
        GLONASS_satelliteTextview.setText("GLONASS:");
        GALILEO_satelliteTextview.setText("GALILEO:");
        BeiDou_satelliteTextview.setText("BeiDou:");
        system1.setText("");
        ID1.setText("");
        elevation1.setText("");
        azimuth1.setText("");
        SNR1.setText("");
        signal_channel1.setText("");
        //归零
        secondsTextView.setText("0.0 s");
        dataItems.clear();
        dataItems_appear.clear();
        getGsv_nmea_data_string2="";
        getGsv_nmea_data_string1="";
        GSA_nmea_data_string1="";
        GSA_nmea_data_string="";
        nmea_data="";
        GGA_string_parts=null;
        VTG_string_parts=null;
        removeAllViewsFromTableLayout();
        //NMEA_isRunning=false;
    }
    //开始计时定位
    private void start_time_location(){
        stop_location_listening();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                if (sendColdCommand()) {
                    // 如果sendWarmCommand执行成功,显示成功提示
                    showSuccessToast();
                }
                startLocationUpdates();
                isRunning = true;//定位运行标识符
                start_time();//开始计时
            }
        }, 500);
    }
    //判断是否打开数据
    public boolean isMobileDataOn() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetworkInfo != null && activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }
    private void write_nmea_data() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_WRITE_STORAGE);
        } else {
            try {
                //File file_gsv = new File(getExternalFilesDir("txt"), "nmea_data_gsv1.txt");
                File file_nmea = new File(getExternalFilesDir("txt"), "nmea_data1.txt");
                //FileWriter writer_gsv = new FileWriter(file_gsv);
                FileWriter writer_nmea = new FileWriter(file_nmea);
                if (nmea_data != null) {
                    //writer_gsv.write(getGsv_nmea_data_string2);
                    //writer_gsv.close();
                    writer_nmea.write(nmea_data);
                    writer_nmea.close();
                    Toast.makeText(this, "nmea_data 保存成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "nmea_data 为空", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(this, "nmea_data 无法写入", Toast.LENGTH_SHORT).show();
            }
        }
    }
    //系统解析
    private String system_string_transformation(String system_string)
    {
        if(system_string.equals("GB")) {return "BeiDou";}
        if(system_string.equals("GP")) {return "GPS";}
        if(system_string.equals("GA")) {return "GALILEO";}
        if(system_string.equals("GQ")) {return "QZSS";}
        if(system_string.equals("GI")) {return "NAVIC";}
        if(system_string.equals("GL")) {return "GLONASS";}
        if(system_string.equals("GN")) {return "CMSS";}
        else{return "";}
    }
    //信道解析
    private String signal_channel_transformation(String system_string,String number) {
        if(system_string.equals("GP")) {
            return GPS_signal_channel[Integer.parseInt(number,16)];
        }
        else if(system_string.equals("GA")) {
            return GALILEO_signal_channel[Integer.parseInt(number,16)];
        }
        else if(system_string.equals("GL")) {
            return GLONASS_signal_channel[Integer.parseInt(number,16)];
        }
        else if(system_string.equals("GB")){
            return BeiDou_signal_channel[Integer.parseInt(number,16)];
        }
        else if(system_string.equals("GQ")){
            return QZSS_signal_channel[Integer.parseInt(number,16)];
        }
        else if(system_string.equals("GI")){
            return NavIC_signal_channel[Integer.parseInt(number,16)];
        }
        else{
            return "L";
        }
    }
    private void location_satellite_appear(String system,String satellite_string)
    {
        if(system.equals("1")){GPS_satelliteTextview.setText("GPS:"+satellite_string);}
        else if(system.equals("2")){GLONASS_satelliteTextview.setText("GLONASS:"+satellite_string);}
        else if(system.equals("3")){GALILEO_satelliteTextview.setText("GALILEO:"+satellite_string);}
        else if(system.equals("4")){BeiDou_satelliteTextview.setText("BeiDou:"+satellite_string);}
    }

    private int dip2px(Context context, int dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    //清空tableLayout
    private void removeAllViewsFromTableLayout() {
        if (tableLayout!= null) {
            tableLayout.removeAllViews();
        }
    }

}

 页面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!-- 使用GradientDrawable设置渐变背景 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:paddingTop="0dp"
        android:visibility="visible"
        tools:ignore="UseSwitchCompatOrMaterialXml">

        <ProgressBar
            android:id="@+id/progressBar5"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#E2DADA"
            android:indeterminateTint="#787474" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1C5A61"
        android:orientation="horizontal"
        android:paddingTop="0dp"
        android:visibility="visible"
        tools:ignore="UseSwitchCompatOrMaterialXml">

        <TextView
            android:id="@+id/firstLocationLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="0dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:text=""
            android:textColor="#FFFFFF"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/seconds_text_view"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="6dp"
            android:paddingBottom="5dp"
            android:text="0.0 s"
            android:textColor="#FDFCFC"
            android:textSize="20sp" />

        <Switch
            android:id="@+id/locationlistener_switch"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="42dp"
        android:background="#E8EAEF"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="0dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/longitudeTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif"
                android:paddingTop="0dp"
                android:text="经度:"
                android:textAllCaps="false"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#373434"
                android:textSize="15dp"
                android:typeface="normal" />

            <TextView
                android:id="@+id/latitudeTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:paddingTop="0dp"
                android:text="纬度:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/altitudeTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="海拔:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/speedTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="速度:"
                android:textColor="#373434"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="0dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/PDOPTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="位置精度因子:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/HDOPTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="水平精度因子:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/VDOPTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="垂直精度因子:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/stateTextview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="0dp"
                android:text="定位状态:"
                android:textColor="#373434"
                android:textSize="15dp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="200dp"
            android:layout_marginTop="90dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/GALILEO_satelliteTextview"
                android:layout_width="160dp"
                android:layout_height="40dp"
                android:paddingTop="0dp"
                android:text="GALILEO:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/BeiDou_satelliteTextview"
                android:layout_width="160dp"
                android:layout_height="40dp"
                android:layout_marginTop="0dp"
                android:text="BeiDou:"
                android:textColor="#373434"
                android:textSize="15dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="90dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/GPS_satelliteTextview"
                android:layout_width="160dp"
                android:layout_height="40dp"
                android:paddingTop="0dp"
                android:text="GPS:"
                android:textColor="#373434"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/GLONASS_satelliteTextview"
                android:layout_width="160dp"
                android:layout_height="40dp"
                android:layout_marginTop="0dp"
                android:text="GLONASS:"
                android:textColor="#373434"
                android:textSize="15dp" />
        </LinearLayout>
    </RelativeLayout>

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="210dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="0dp"
        android:stretchColumns="*">
        <!-- @color/blackble设置背景 -->

        <TableRow
            android:id="@+id/tRowColumTitle"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@color/black">

            <LinearLayout android:orientation="horizontal">

                <TextView
                    android:id="@+id/system"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="0dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="2"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="定位系统"
                    android:textSize="10sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/satellite_ID"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="ID"
                    android:textSize="10sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/elevation"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="1.5"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="俯仰角"
                    android:textSize="10sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/azimuth"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="1.5"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="方位角"
                    android:textSize="10sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/SNR"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="1.5"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="载噪比"
                    android:textSize="10sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/signal_chaanel"
                    android:layout_width="0dp"
                    android:layout_height="36dp"
                    android:layout_marginTop="1dp"
                    android:layout_marginRight="0dp"
                    android:layout_marginBottom="2dp"
                    android:layout_weight="1.4"
                    android:background="@color/white"
                    android:gravity="center"
                    android:padding="6dp"
                    android:singleLine="true"
                    android:text="信道"
                    android:textSize="10sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </TableRow>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="275dp"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginBottom="0dp"
            android:background="#FFFFFF"
            android:gravity="center"
            android:orientation="vertical">

            <TableRow
                android:id="@+id/tRowColum"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white">
                <LinearLayout android:orientation="horizontal">

                    <TextView
                        android:id="@+id/system1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="0dp"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="0dp"
                        android:layout_marginBottom="0dp"
                        android:layout_weight="2"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/ID1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="0dp"
                        android:layout_marginBottom="0dp"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/elevation1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="1dp"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1.5"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/azimuth1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="1dp"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1.5"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/SNR1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="1dp"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1.5"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/signal_channel1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="0dp"
                        android:layout_marginRight="1.6dp"
                        android:layout_marginBottom="2dp"
                        android:layout_weight="1.4"
                        android:background="@color/white"
                        android:gravity="center"
                        android:padding="6dp"
                        android:text=""
                        android:textSize="10sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </TableRow>
        </ScrollView>
    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="520dp"
        android:layout_marginBottom="0dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/aid_location_button"
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:background="@drawable/oval_button"
            android:text="开始辅助定位计时"
            android:textColor="#000000" />

        <Button
            android:id="@+id/location_button"
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@drawable/oval_button"
            android:text="开始无辅助定位计时"
            android:textColor="#000000" />

        <Button
            android:id="@+id/save_nmea_data"
            android:layout_width="282dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:background="@drawable/oval_button"
            android:text="保存NMEA数据"
            android:textColor="#000000" />

    </LinearLayout>




</RelativeLayout>

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐