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;}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/539278.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bootstrap企业网站前端模板

介绍 企业网站前端模板 软件架构 前端所用技术html/css/js/jquery 前端框架bootstrap 安装教程 浏览器本地路径访问发布到服务器比如&#xff08;tomcat/nginx等&#xff09;云服务器/虚拟机 网站效果图 网站预览 点击预览 源码地址 https://gitee.com/taisan/company…

从金蝶云星空到钉钉通过接口配置打通数据

从金蝶云星空到钉钉通过接口配置打通数据 对接系统金蝶云星空 金蝶K/3Cloud&#xff08;金蝶云星空&#xff09;是移动互联网时代的新型ERP&#xff0c;是基于WEB2.0与云技术的新时代企业管理服务平台。金蝶K/3Cloud围绕着“生态、人人、体验”&#xff0c;旨在帮助企业打造面…

【深度学习笔记】9_5 多尺度目标检测

注&#xff1a;本文为《动手学深度学习》开源内容&#xff0c;部分标注了个人理解&#xff0c;仅为个人学习记录&#xff0c;无抄袭搬运意图 9.5 多尺度目标检测 在9.4节&#xff08;锚框&#xff09;中&#xff0c;我们在实验中以输入图像的每个像素为中心生成多个锚框。这些…

如何配置Apache的反向代理

目录 前言 一、反向代理的工作原理 二、Apache反向代理的配置 1. 安装Apache和相关模块 2. 配置反向代理规则 3. 重启Apache服务器 三、常见的使用案例 1. 负载均衡 2. 缓存 3. SSL加密 总结 前言 随着Web应用程序的不断发展和扩展&#xff0c;需要处理大量的请求和…

腾讯云服务器CVM_云主机_云计算服务器_弹性云服务器

腾讯云服务器CVM提供安全可靠的弹性计算服务&#xff0c;腾讯云明星级云服务器&#xff0c;弹性计算实时扩展或缩减计算资源&#xff0c;支持包年包月、按量计费和竞价实例计费模式&#xff0c;CVM提供多种CPU、内存、硬盘和带宽可以灵活调整的实例规格&#xff0c;提供9个9的数…

部署快捷、使用简单、推理高效!大模型部署和推理框架 Xinference 来了!

今天为大家介绍一款大语言模型&#xff08;LLM&#xff09;部署和推理工具——Xinference[1]&#xff0c;其特点是部署快捷、使用简单、推理高效&#xff0c;并且支持多种形式的开源模型&#xff0c;还提供了 WebGUI 界面和 API 接口&#xff0c;方便用户进行模型部署和推理。 …

3.1_9 基本分段存储管理

文章目录 3.1_9 基本分段存储管理&#xff08;一&#xff09;分段&#xff08;二&#xff09;段表&#xff08;三&#xff09;地址变换&#xff08;四&#xff09;分段、分页管理的对比 总结 3.1_9 基本分段存储管理 &#xff08;一&#xff09;分段 进程的地址空间&#xff1a…

实现基本的登录功能

一、登录功能的前端处理过程 1、导入项目所需的图片和CSS等静态文件 参考代码存放client节点的/opt/code目录下 执行如下命令&#xff1a; [rootclient ~]# cp -r /opt/code/kongguan_web/src/assets/* /root/kongguan_web/src/assets/ 将参考代码中的css、icon、images等文…

Linux-vim显示乱码

Linux运维工具-ywtool 目录 一.问题二.解决2.1 编辑VIM的配置文件2.2 添加以下内容 一.问题 用vim编辑的时候,中文显示乱码 二.解决 2.1 编辑VIM的配置文件 vim ~/.vimrc #如果这个文件不存在,创建一个即可2.2 添加以下内容 添加完成以后就不会在出现中文乱码了 set fil…

Nginx、LVS、HAProxy工作原理和负载均衡架构

当前大多数的互联网系统都使用了服务器集群技术&#xff0c;集群是将相同服务部署在多台服务器上构成一个集群整体对外提供服务&#xff0c;这些集群可以是 Web 应用服务器集群&#xff0c;也可以是数据库服务器集群&#xff0c;还可以是分布式缓存服务器集群等等。 在实际应用…

IDEA中导入eclipse运行的java项目

本篇文章主要的内容是在IDEA中导入eclipse运行的java项目&#xff0c;亲测有效。 话不多说&#xff0c;直接开整。 前提&#xff1a;先打开idea软件&#xff0c;界面如下&#xff1a; 开始按下方步骤依次走即可。 1、file --> new --> Project from Existing Sources..…

GoLang:云原生时代致力于构建高性能服务器的后端语言

Go语言的介绍 概念 Golang&#xff08;也被称为Go&#xff09;是一种编程语言&#xff0c;由Google于2007年开始设计和开发&#xff0c;并于2009年首次公开发布。Golang是一种静态类型、编译型的语言&#xff0c;旨在提供高效和可靠的软件开发体验。它具有简洁的语法、高效的编…