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. 《Rust权威指南》学习笔记——4. 认识所有权

    Rust权威指南学习笔记--认识所有权 什么是所有权 1. 所有权规则 Rust中的每一个值都有一个对应的变量作为它的所有者. 在同一时间内,值有且仅有一个所有者. 当所有者离开自己的作用域时,它持有 ...

  2. Python下使用argparse模块的脚本参数配置

    python的一个用于命令行参数解析的模块,其专业解释已经有很多了,可以去详查,不做赘述,仅谈谈自己的一些理解. 为什么要用argparse模块来为代码导入参数或者文件路径呢?如果是一个简单的项目,输 ...

  3. PPT导出高分辨率tif图片——用于学术论文

    PPT导出的图片默认分辨率只有96dpi,但要到印刷品要求的图片分辨率最好是300dpi,学术论文也需要高清晰度的图片.要让PPT导出的图片分辨率达到300dpi,其实可以不用PS,直接修改系统注册表 ...

  4. C++ NRV优化

    NRV,named return value 某个函数返回一个对象时,在外部接收 A a = Fun(); 会调用复制构造函数 会损失性能 为何不能将函数Fun定义成传入引用a,然后直接在a上进行操作 ...

  5. SQLSERVER 根据一个库的视图在另一个库中生成一张表

    select * into VPsiOuntStockBill from [KshDbPro].dbo.VPsiOuntStockBill

  6. vue-quill-editor 图片上传处理

    <template> <div class="quill-editor"> <!-- 图片上传组件辅助--> <el-upload cla ...

  7. VUE学习-计算属性与监听器

    计算属性与监听器 计算属性 当 vm.message 发生改变时,所有依赖 vm.reversedMessage 的绑定也会更新 <div id="example"> ...

  8. VMWare 12 Mac 10.11 XCode 7.3 Ipad真机调试重要问题总结

    XCode 7.3可以不需要每年缴费而直接调试IOS应用,测试如下: 1,安装Mac 10.11在VMWare12上,网上有很多例子.注意: 1.1,虚拟机设置中USB为USB2.0,不能是3.0或其 ...

  9. 大规模人脸分类—allgather操作(2)

    腾讯开源人脸识别训练代码TFace 中关于all_gather层的实现如下.接下来解释为什么backward要进行reduce相加操作. https://github.com/Tencent/TFac ...

  10. 钉钉-E应用开发初体验(企业内部应用)

    首先要创建应用,如何创建参考 https://open-doc.dingtalk.com/microapp/bgb96b/gt5d6a 下载 钉钉E应用服务端demo   git clone http ...