一、项目思路
二、项目源码
package com.example.myself; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.pm.PackageManager; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.os.Bundle; import android.view.WindowManager; import android.widget.TextView; import java.util.Iterator; import java.util.List; public class MainActivity extends AppCompatActivity { private TextView text;//定义用于显示LocationProvider名称的TextView组件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示 text = (TextView) findViewById(R.id.provider); //获取显示LocationProvider名称的TextView组件 LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); //权限检查 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 // Activity#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 Activity#requestPermissions for more details. return; } locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,//指定GPS定位的提供者 1000,//间隔时间 1,//定位间隔1米 new LocationListener() {//监听GPS定位信息是否改变 @Override public void onLocationChanged(Location location) {//GPS信息发生改变时回调 } @Override public void onStatusChanged(String provider, int status, Bundle extras) {//GPS状态改变时回调 } @Override public void onProviderEnabled(String provider) {//定位提供者启动时回调 } @Override public void onProviderDisabled(String provider) {//定位提供者关闭时回调 } } ); Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//获取最新定位信息 locationUpdates(location);//将最新的定位信息传递给LocationUpdates()方法 } public void locationUpdates(Location location){ if(location!=null){//location用于保存定位信息 StringBuilder stringBuilder=new StringBuilder();//创建字符串构建器,记录定位信息 stringBuilder.append("您的定位信息是:\n"); stringBuilder.append("经度:"); stringBuilder.append(location.getLongitude()); stringBuilder.append("\n纬度:"); stringBuilder.append(location.getLatitude()); text.setText(stringBuilder.toString());//将定位信息显示到页面上 }else{ text.setText("没有获取到GPS信息"); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="16dp" android:paddingRight="64dp" android:paddingLeft="64dp" android:paddingBottom="16dp" android:background="@drawable/blue" tools:context=".MainActivity"> //获取显示信息 <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="26sp" android:textColor="#000000" android:textStyle="bold" android:layout_above="@+id/provider" android:text="可用LocationProvider"/> //获取提示结果 <TextView android:id="@+id/provider" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:lineSpacingExtra="5dp" android:layout_centerHorizontal="true" android:textSize="26sp" android:textStyle="bold"/> </RelativeLayout>
AndroidManifest:(增加权限)
三、运行截图
四、遇到的问题
输出没有获取到GPS信息
问题:所在的地方没有GPS信号,找一个空旷的地方进行测样获取
解决时间:8分钟