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

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读