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

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo