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. 日常开发记录-this.$message,this.$prompt,交换弹窗确定和取消按钮的位置,确定在左,取消在右

    代码: <template> <el-button type="text" @click="open">点击打开 Message Box ...

  2. C# 当页面有很多选择条件时的处理方式

    如下图,用户可能输入很多条件 在后端的处理方式: 使用键值对 private Dictionary<string, string> CreatSearchPara() { Dictiona ...

  3. CMMI-QA工作流程(角色区分)

    qa 是如何工作的,如何保证产品质量的? 首先制定质量保证计划->根据过程清单和产品清单对组织级和项目级内容进行检查->不符合项记录在不符合项问题记录表中.反馈项目精力,跟踪问题知道问题解 ...

  4. django的注意事项

    1 允许 0.0.0.0:8000 ALLOWED_HOSTS = ['*']     2 py文件的import ,要新建一个文件夹 如tool,与manage.py同级 再去导入

  5. MacOS系统下selenium之火狐浏览器驱动firefoxdriver安装

    https://blog.csdn.net/weixin_44107321/article/details/106452468

  6. python语法中的左值、右值和字符

    位置决定语义 在下面的python代码中,忽略掉语法错误,源码中同样一个单词tsecer在不同的位置有不同的意义 import之后 在import之后的tsecer是作为一个简单的字面字符串来处理:这 ...

  7. Twitter Bootstrap Grid System

    Bootstrap是什么?Bootstrap是一个用来快速并且容易的开发web页面,强大的前端框架.它包含html和css基本设计模板,包含通用用户界面组件,如:排版,形式,按钮,表格,导航,下拉框, ...

  8. SQL Server 机器学习服务-概述与实战(转)

    原帖地址:https://d-bi.gitee.io/sqlserver-ml-services/ 新年第一篇,去旧迎新.本文内容,既旧也新.旧之处在于,SQL Server 机器学习服务是微软在SQ ...

  9. TCP 粘包/拆包的原因及解决方法?

    TCP是以流的方式来处理数据,一个完整的数据包可能会被TCP拆分成多个包进行发送,也可能把多个小的包封装成一个大的数据包.由于TCP数据包之间没有边界保护,所以当发生粘包或拆包时,接收端难以从数据流中 ...

  10. c# TPL中的数据竞争-Data Race

    例一: Parallel.For(1, arraySize, i => { ary[i] = ary[i] + ary[i - 1]; });如下: object _lock = new obj ...