Hello, I am a student who is developing Android. I would like to know the latitude and longitude values of the current location when any event occurs using gps. I copied and pasted a lot of code through Googleing The chords that work properly None.
As far as I know, the permission problem has become more complicated as Android API version is over 23 I'm afraid this is the problem. Is there any way to obtain latitude and longitude values using gps when a specific event occurs...?
Below is activity's
that will be turned on when building the app for the first timePartial code of the onCreate function.
Log.d("Main", "onCreate");
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// GPS provider availability
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Network provider availability
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.d("Main", "isGPSEnabled=" + isGPSEnabled);
Log.d("Main", "isNetworkEnabled=" + isNetworkEnabled);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lng = location.getLongitude();
Toast.makeText(BT_List.this, lat + " " + lng, Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
// // Register the listener with the Location Manager to receive location updates
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.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.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
// Obtain Location Manually
String locationProvider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if (lastKnownLocation != null) {
double lng = lastKnownLocation.getLatitude();
double lat = lastKnownLocation.getLatitude();
Log.d("Main", "longtitude=" + lng + ", latitude=" + lat);
}
Please refer to the questions below.
http://hashcode.co.kr/questions/2573/ Android-Studio-Simple-Light Hardness-Only Display-How
© 2024 OneMinuteCode. All rights reserved.