manrajt
manrajt

Reputation: 45

GPS Location Only Works When App Is First Opened

I am trying to obtain a user's longitude and latitude when a button is clicked.

The following code obtains the GPS when the app is first installed, however, when the app is closed and reopened, it does not obtain any location.

I'm not sure if I incorrectly wrote the code to obtain the GPS location or I am missing something. Any advice would be appreciated.

public class MainActivity extends AppCompatActivity {
    private Button button;
    private EditText editText;
    private LocationManager locationManager
    private LocationListener locationListener;

    private double longitude;
    private double latitude;
    private TextView textView;


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

        button = (Button) findViewById((R.id.button2));
        editText = (EditText) findViewById((R.id.editText2));

        //My code
        textView = (TextView) findViewById((R.id.textView2));

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                textView.append(("\n" + location.getLatitude() + " " + location.getLongitude()));
            }
            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {


            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent intent = new Intent((Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                startActivity(intent);
            }
        };
        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[]{
                       android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                       android.Manifest.permission.INTERNET
               }, 10);
                //return;
            }
        }
        else {
            configureButton();
        }
        button.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                configureButton();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 10:
                if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    configureButton();
                }
                return;
        }
    }

    private void configureButton() {
       /* button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);

            }
        });*/
        locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);


     }
}

Upvotes: 2

Views: 58

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

Just just need to request location updates if the user has already granted the Location permission:

    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[]{
                   android.Manifest.permission.ACCESS_FINE_LOCATION,android.Manifest.permission.ACCESS_COARSE_LOCATION,
                   android.Manifest.permission.INTERNET
           }, 10);

        } else {
            //Added:
            locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
        }
    }
    else {
        locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
    }

Upvotes: 1

Related Questions