Android 监听卫星导航系统状态及卫星测量数据变化

2024-03-15 20:44

本文主要是介绍Android 监听卫星导航系统状态及卫星测量数据变化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
源码

package com.android.circlescalebar;import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.GnssClock;
import android.location.GnssMeasurement;
import android.location.GnssMeasurementsEvent;
import android.location.GnssStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;
import com.android.circlescalebar.data.GnssRawData;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;public class GpsActivity extends AppCompatActivity {public static final int LOCATION_CODE = 525;private LocationManager locationManager;private String locationProvider = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_gps);getLocation();GnssTest();}private void getLocation() {locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);//获取所有可用的位置提供器List<String> providers = locationManager.getProviders(true);if (providers.contains(LocationManager.GPS_PROVIDER)) {//如果是GPSlocationProvider = LocationManager.GPS_PROVIDER;} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {//如果是NetworklocationProvider = LocationManager.NETWORK_PROVIDER;} else {Intent i = new Intent();i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);startActivity(i);}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限)if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED|| ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {//请求权限ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE);} else {//监视地理位置变化locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);Location location = locationManager.getLastKnownLocation(locationProvider);if (location != null) {//输入经纬度Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show();}}} else {//监视地理位置变化locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);Location location = locationManager.getLastKnownLocation(locationProvider);if (location != null) {//不为空,显示地理位置经纬度Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show();}}}private void GnssTest() {if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {return;}locationManager.registerGnssStatusCallback(mGNSSCallback, null); //卫星导航系统的状态发生变化时被调用locationManager.registerGnssMeasurementsCallback(mGnssMeasurementsListener, null); //卫星测量数据// 请求位置跟新,所有监听器的回调都必须通过此方法locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListener);}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);switch (requestCode) {case LOCATION_CODE:if (grantResults.length > 0 && grantResults[0] == getPackageManager().PERMISSION_GRANTED&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {Toast.makeText(this, "申请权限", Toast.LENGTH_LONG).show();try {List<String> providers = locationManager.getProviders(true);if (providers.contains(LocationManager.NETWORK_PROVIDER)) {//如果是NetworklocationProvider = LocationManager.NETWORK_PROVIDER;} else if (providers.contains(LocationManager.GPS_PROVIDER)) {//如果是GPSlocationProvider = LocationManager.GPS_PROVIDER;}//监视地理位置变化locationManager.requestLocationUpdates(locationProvider, 3000, 1, locationListener);Location location = locationManager.getLastKnownLocation(locationProvider);if (location != null) {//不为空,显示地理位置经纬度Toast.makeText(GpsActivity.this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show();}} catch (SecurityException e) {e.printStackTrace();}} else {Toast.makeText(this, "缺少权限", Toast.LENGTH_LONG).show();finish();}break;}}@Overrideprotected void onDestroy() {super.onDestroy();locationManager.removeUpdates(locationListener);}public LocationListener locationListener = new LocationListener() {// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}// Provider被enable时触发此函数,比如GPS被打开@Overridepublic void onProviderEnabled(String provider) {}// Provider被disable时触发此函数,比如GPS被关闭@Overridepublic void onProviderDisabled(String provider) {}//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发@Overridepublic void onLocationChanged(Location location) {if (location != null) {//不为空,显示地理位置经纬度Toast.makeText(GpsActivity.this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show();}}};GnssStatus.Callback mGNSSCallback = new GnssStatus.Callback() {@Overridepublic void onSatelliteStatusChanged(@NonNull GnssStatus status) {super.onSatelliteStatusChanged(status);// get satellite countint satelliteCount = status.getSatelliteCount();int BDSatelliteCount = 0;int BDSInFix = 0;TextView sateCount = findViewById(R.id.sateCount);TextView satebeidou = findViewById(R.id.satebeidou);TextView satebeidoulocation = findViewById(R.id.satebeidoulocation);sateCount.setText("共收到卫星信号:" + satelliteCount + "个");if(satelliteCount > 0) {for (int i = 0; i < satelliteCount; i++) {// get satellite typeint type = status.getConstellationType(i);if(GnssStatus.CONSTELLATION_BEIDOU == type) {// increase if type == BEIDOUBDSatelliteCount++;if (status.usedInFix(i)) {BDSInFix++;}}}satebeidou.setText("北斗卫星有:" + BDSatelliteCount + "个");satebeidoulocation.setText("用于定位的北斗卫星有:" + BDSInFix + "个");}}};private final GnssMeasurementsEvent.Callback mGnssMeasurementsListener = new GnssMeasurementsEvent.Callback() {@Overridepublic void onGnssMeasurementsReceived(GnssMeasurementsEvent eventArgs) {super.onGnssMeasurementsReceived(eventArgs);GnssClock clock = eventArgs.getClock();Collection<GnssMeasurement> measurements = eventArgs.getMeasurements();List<Mea> mLst = new ArrayList<>();for (GnssMeasurement measurement : measurements) {GnssRawData data = new GnssRawData(measurement, clock);mLst.add(new Mea(data.getPRN(), data.getCarrierFrequencyHZ(), data.getPseudorange(),clock.getTimeNanos(), clock.getFullBiasNanos(), measurement.getReceivedSvTimeNanos()));}Collections.sort(mLst);StringBuilder builder = new StringBuilder();Locale locale = Locale.getDefault();builder.append(String.format(locale, "%5s%20s%20s\n", "SV", "Carrier(MHz)", "Value"));for (Mea m : mLst) {builder.append(String.format(locale, "%5s%20.3f%20.3f\n", m.getPRN(),m.getCarrier(), m.getPseudorange()));}runOnUiThread(new Runnable() {@Overridepublic void run() {TextView sate = findViewById(R.id.sate);sate.setText(builder.toString());}});}@Overridepublic void onStatusChanged(int status) {super.onStatusChanged(status);}};/*** 将 measurement排序(为了查看是否有多频)*/class Mea implements Comparable<Mea> {private final String prn;private final double carrier;private final double pseudorange;private final double TimeNanos;private final double FullBiasNanos;private final double ReceivedSvTimeNanos;public Mea(String prn, double carrier, double pseudorange, double TimeNanos,double FullBiasNanos, double ReceivedSvTimeNanos) {this.prn = prn;this.carrier = carrier;this.pseudorange = pseudorange;this.TimeNanos = TimeNanos;this.FullBiasNanos = FullBiasNanos;this.ReceivedSvTimeNanos = ReceivedSvTimeNanos;}@Overridepublic int compareTo(Mea o) {return this.prn.compareTo(o.prn);}public String getPRN() {return prn;}public double getCarrier() {return carrier;}public double getPseudorange() {return pseudorange;}public double getTimeNanos() {return TimeNanos;}public double getFullBiasNanos() {return FullBiasNanos;}public double getReceivedSvTimeNanos() {return ReceivedSvTimeNanos;}}
}

这篇关于Android 监听卫星导航系统状态及卫星测量数据变化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813215

相关文章

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Redis分片集群、数据读写规则问题小结

《Redis分片集群、数据读写规则问题小结》本文介绍了Redis分片集群的原理,通过数据分片和哈希槽机制解决单机内存限制与写瓶颈问题,实现分布式存储和高并发处理,但存在通信开销大、维护复杂及对事务支持... 目录一、分片集群解android决的问题二、分片集群图解 分片集群特征如何解决的上述问题?(与哨兵模

浅析如何保证MySQL与Redis数据一致性

《浅析如何保证MySQL与Redis数据一致性》在互联网应用中,MySQL作为持久化存储引擎,Redis作为高性能缓存层,两者的组合能有效提升系统性能,下面我们来看看如何保证两者的数据一致性吧... 目录一、数据不一致性的根源1.1 典型不一致场景1.2 关键矛盾点二、一致性保障策略2.1 基础策略:更新数

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级