package com.example.myapplication;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import java.util.List;

import static com.example.myapplication.MainActivity.tag;

public class FourActivity extends Activity {
public static final int SHOW_LOCATION = 0;
private TextView positionTextView;
private LocationManager locationManager;
private String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four);
positionTextView = (TextView) findViewById(R.id.btfour);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

List<String> providerList = locationManager.getProviders(true);

if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;

Log.d(tag, "测试GPS");
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;

Log.d(tag, "测试NETWORK");
} else {

Log.d(tag, "没有可用的位置提供器");

return;
}

@SuppressLint("MissingPermission")Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {

showLocation(location);
Log.d(tag, "显示当前设备的位置信息");
} else {

Log.d(tag, "位置信息是空");
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.d(tag, "检测权限");
return;
}
locationManager.requestLocationUpdates(provider, 2000, 1,
locationListener);

}

protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
// 关闭程序时将监听器移除
locationManager.removeUpdates(locationListener);
};
}

LocationListener locationListener = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onLocationChanged(Location location) {
// 更新当前设备的位置信息
showLocation(location);
}
};

public void showLocation(Location location) {
String currentPosition="纬度"+location.getLatitude()+"\n"+"经度"+location.getLongitude();
positionTextView.setText(currentPosition);
}

}

获取gps的更多相关文章

  1. GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告

    实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...

  2. android 获取GPS定位

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  3. 获取gps信息

    (一)plist修改 添加如下变量 (二)新建视图用来启动Gps 此视图控制器继承CLLocationManagerDelegate #import <UIKit/UIKit.h> #im ...

  4. MacOS下Terminal获取GPS经纬度坐标

    通过命令行直接获取经纬度坐标MacOS 首先下载WhereAmI,最新版本: https://github.com/robmathers/WhereAmI/releases/download/v1.1 ...

  5. html获取gps坐标

    <script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(n ...

  6. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  7. 通过图片获取gps地理位置

    别人说通过一张照片就可以定位你的位置,看来个视频,仔细研究了一下自己的照片没想到真的可以做到,想想真的有点可怕. 如何通过一张照片去定位这张照片的经纬度下面我以我手机中的照片为例. 我们通过pytho ...

  8. js获取 gps坐标

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(pos) { model.address. ...

  9. 使用SerialPort 读取外置GPS信息和使用GeoCoordinateWatcher获取内置gps的信息

    简介最近工作中需要读取gps设备的信息,平板本身有内置的gps设备,但是精度不够,就又添加了一个外置的gps.对于外置的gps,我们主要通过SerialPort类来获得串口的信息,然后对接收到的内容进 ...

  10. Android Service获取当前位置(GPS+基站)

    需求详情:1).Service中每隔1秒执行一次定位操作(GPS+基站)2).定位的结果实时显示在界面上(要求得到经度.纬度)技术支持:1).获取经纬度通过GPS+基站获取经纬度,先通过GPS来获取, ...

随机推荐

  1. 技嘉b75m-d3v在nvme固态安装win7并且oem激活的实现过程

    本篇文章主要讲述了实现台式机主板oem激活win7以及旧平台主板使用nvme固态安装win7的过程 事情的起因是我去年在小黄鱼买了台戴尔n4110,今年买了台惠普银河舰队2代,并且把里面128GB的n ...

  2. APP与Web测试区别

    相同点: WEB 测试和 App 测试从流程上来说,没有区别.都需要经历测试计划方 案,用例设计,测试执行,缺陷管理,测试报告等相关活动.从技术上来说, WEB 测试和 APP 测试其测试类型也基本相 ...

  3. Assembler Instructions with C Expression Operands

    Using the GNU Compiler Collection For gcc version 4.9.3 (GNU Tools for ARM Embedded Processors) In a ...

  4. REMOTE HOST IDENTIFICATION HAS CHANGED!服务器重置后远程连接不上

    问题: 解决: 本地打开shell,重置key

  5. IEC104

    IEC10X协议源码 https://github.com/airpig2011/IEC104 一.源码编译 下载源代码并解压 进入文件夹下test目录,对main.c进行修改 我们使用套接字来实现服 ...

  6. java技术系列(二) 反射

    能够分析类能力的程序称为反射. 反射能力: 在运行中分析类的能力. 在运行中查看对象,例如:编写一个toString方法供所有类使用. 实现通用的数组操作代码. 利用Method对象.

  7. WCF 安全

    服务器端与客户端都安装证书,双方通过证书加密通讯. 配置wsHttpBinding,使用基于消息的用户名验证.首先配置为Windows账户库验证. 服务器端配置: <?xml version=& ...

  8. unity默认管线lightmap

    lightmap采样 https://blog.csdn.net/wodownload2/article/details/94431040

  9. centos7 双网卡同网段双网关配置

    需求: #1.服务器为双网卡: #2.网卡1为互联网 172.16.137.99/24/254 #3.网卡2为旅游专网 172.16.137.97/24/1 #4.互联网路由器为172.16.137. ...

  10. 串口USART(续二)

    通过前面的分析知道,在LPC824中,USART所拥有的配置寄存器有很多,但在一般情况下,如果只是简单地使用收发功能,则只需要使用到配置寄存器CFG.波特率发生器寄存器BRG.中断使能读取和置位寄存器 ...