Search code examples
javaandroidgpslocation

Android Studio - get GPS data


I'm trying to get GPS data from my android phone and in this code I'm just trying to get the Latitude.
I did not use the method onLocationChanged because I only want to get the data once.
I did not get any errors or warnings.

package com.example.gpstryxyz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

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.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 10, this);
        }

        Location loc1 = new Location("gps");
        double value1 = loc1.getLatitude();

        TextView tv = (TextView) findViewById(R.id.gpsTextView);
        tv.setText(String.valueOf(value1));
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

So I build the app on my phone, turn on GPS, go outside, start the app and the output is:

0.0

Solution

  • I would like to answer my own question.
    I did not use the requestPermissions() method in my code above and even had the wrong kind of permission in the if-statement.
    I also did not know how the methods requestLocationUpdates() and the callback method onRequestPermissionsResult() worked. (ctrl + Q helped here)

    package com.example.gpstryxyz;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    import androidx.core.content.ContextCompat;
    
    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.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements LocationListener {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //app seemed to work only with gps, but does not update during runtime, quit and restart app to get new location
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                LocationManager locationManager;
                locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
                //registration (if this call succeeds, it calls the onLocationChanged() method)
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
            } else {
                TextView tv = (TextView) findViewById(R.id.gpsTextView);
                tv.setText(R.string.noPermission);
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) throws SecurityException{
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // permission is granted
                LocationManager locationManager;
                locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
            }
        }
    
        @Override
        public void onLocationChanged(Location location) {
            //the Location object this method brings has the location data in it
            double value1 = location.getLatitude();
            double value2 = location.getLongitude();
            TextView tv = (TextView) findViewById(R.id.gpsTextView);
            tv.setText(getString(R.string.latlonString, String.valueOf(value1), String.valueOf(value2)));
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(MainActivity.this, "gps is activated.....", Toast.LENGTH_LONG).show();
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(MainActivity.this, "gps is NOT activated.....", Toast.LENGTH_LONG).show();
        }
    }