Android GPS get current location

I developed a simple application for Android, and in this simple application I want to get GPS Latitued and Longitued data, it works correctly in my emulator, but when I installed it on a real Android device, it does not work. Please help me solve this problem and this is my code. thanks in advance

public class getLocation extends AppCompatActivity { private Button btnGetLocation; private TextView textGPS; private LocationManager locationManager; private LocationListener locationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_location); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); btnGetLocation = (Button) findViewById(R.id.btnGetLocation); textGPS = (TextView) findViewById(R.id.textViewGps); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { textGPS.setText("lat: "+location.getLatitude()+" long: "+location.getLongitude()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.INTERNET },10); return; } }else{ configureButton(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode){ case 10: if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) configureButton(); return; } } private void configureButton() { btnGetLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { locationManager.requestLocationUpdates("gps", 0, 0, locationListener); } }); } } 

Screenshot from the emulator: enter image description here

Screenshot from mobile: enter image description here

+5
source share
3 answers

You only ask for GPS ... Your device needs to make contact with the satellites! In space! Go out and test it! This is a common problem when testing a code that uses only GPS; it does not work very well indoors.

In addition, you should not use hardcoded "gps", you should use:

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

Regarding the use of the FusedLocationProviderAPI, you will lose some accuracy, but it will significantly improve battery performance, since it is heavily dependent on network location and uses GPS only when absolutely necessary. For a complete example of how to use it, see here: fooobar.com/questions/295183 / ...

+4
source

add this to your code. When this application launches indoors, the last known location.

  // Get the location from the given provider Location location = locationManager .getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 1000, 1, this); 
+2
source

The Google Play services location APIs are preferred over the Android location APIs (android.location) as a way to add location information to your application.

You can find an example of how to use it on the right here.

PS Remember to check if permissions are allowed on your device.

+1
source

Source: https://habr.com/ru/post/1246759/


All Articles