1.权限申请
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qin.longitude">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- These 3 are required to enable getting cooords -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- The above 3 are required to enable getting cooords -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.
package com.qin.longitude;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get user location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Keep track of user location.
// Use callback/listener since requesting immediately may return null location.
// IMPORTANT: TO GET GPS TO WORK, MAKE SURE THE LOCATION SERVICES ON YOUR PHONE ARE ON.
// FOR ME, THIS WAS LOCATED IN SETTINGS > LOCATION.
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(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.NETWORK_PROVIDER, 1000, 10, new Listener());
// Have another for GPS provider just in case.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new Listener());
// Try to request the location immediately
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location != null){
handleLatLng(location.getLatitude(), location.getLongitude());
}
Toast.makeText(getApplicationContext(),
"Trying to obtain GPS coordinates. Make sure you have location services on.",
Toast.LENGTH_SHORT).show();
}
/**
* Handle lat lng.
*/
private void handleLatLng(double latitude, double longitude){
Log.v("TAG", "-----------(" + latitude + "," + longitude + ")");
}
/**
* Listener for changing gps coords.
*/
private class Listener implements LocationListener {
public void onLocationChanged(Location location) {
double longitude = location.getLongitude();
double latitude = location.getLatitude();
handleLatLng(latitude, longitude);
}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
}
注意我这里没有写动态申请地理权限的代码,要自己在设置里给应用分配gps权限先,如果有需要,可以用easypermision自行添加动态申请的代码
3.效果: