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. LeetCode习题集

    作为一名本科计算机科学与技术的学生,<数据结构与算法><算法分析与设计>这两门课没有学到特别好 上了研究生之后感觉,这些东西需要重新拾起 于是,我找了一个平台,LeetCode ...

  2. 遍历List时删除元素导致List抛出java.util.ConcurrentModificationException异常

    1 public static void main(String[] args) { 2 List<String> list = new ArrayList<String>() ...

  3. ES得分

    一.概念 1.ES主要用于搜索 2.搜索要把更有相关性的结果展示出来 3.对一个文档评分,相关性越大,评分越高 4.打分的本质是排序 二.评分规则 1.ES5之前,默认评分规则是TF-IDF,这是信息 ...

  4. SecureCRT保存日志

    1.打开Options->Session Options...,选择LogFile 2.Log file name格式 %H_%S_%Y%M%D-%h%m%s.log 参数说明: %H---主机 ...

  5. laravel视图响应

    1.输出json $data = [ 'id' => 1, 'name' => 'jack', ]; return response()->json($data); 2.重定向 // ...

  6. Xilinx-英文缩写

    BEL: Basic Element of Logic, BEL是最底层的基本元素,也可以叫atomic unit(原子单位),BEL是FPGA中最小.不可分割的组件. ACP:Accelerator ...

  7. Ubuntu网络重置

    Ubuntu网络重置 >重置网络 sudo service network-manager stop sudo rm /var/lib/NetworkManager/NetworkManager ...

  8. K8S实现不同节点POD获取不同IP

    背景介绍 某混合云场景k8s,云上和云下的node,需要将同一个域名解析到不同的IP 方案 利用Coredns+2个第三方插件,fwdpolicy,conditional 编译Coredns(在win ...

  9. Rest-Assured:发送PATCH请求:更新Hello-imook(java.lang.IllegalArgumentException: Invalid number of path parameters. Expected 2, was 0. Undefined path parameters are: owner, repo.)

    代码: /*     * 发送PATCH请求:更新Hello-imook     */    @Test    public void test003_PatchMethod() {        S ...

  10. C语言 数据编码方式

    一.整形数据类型 1.无符号数的编码 无符号数指的是整个机器字长的全部位数均表示数值位. 我们用函数      来进行运算.(B2U是Binary to Unsigned的缩写,长度为w),x代表为0 ...