2

My issue: I am attempting to create an app that can be used university wide. It requires constant access to location. I followed a tutorial online and completed the code underneath. This code works perfectly for my android emulator, but does not work on my device. I get the permission request on my device, but nothing gets updated. Now, the strange thing is, if I use a mock location app on my device, this app DOES work. But only with the mock location app.

Does anyone have any suggestions on how to fix this? (The xml file is just a single textview. Like i said, I was just testing this first)

package com.example.dylan.locationexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private LocationManager locationManager;
    private LocationListener locationListener;

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

        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("Location: ", location.toString());

                TextView tv = findViewById(R.id.textView);
                tv.setText(tv.getText() + Double.toString(location.getLatitude()) + "\t" + Double.toString(location.getLongitude()) + "\n");

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };



        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_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.

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

        }else{

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

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


            }
        }
    }


}

2 Answers2

3

Please ensure you have the following in the manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

And enable gps on your physical device.

If you still face the problem, check the permission settings on your mobile phone for the app. You may have denied permission to access location.

Here is my code am using:

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    com.google.android.gms.location.LocationListener mLocationListener = new com.google.android.gms.location.LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            listener.onLocation(location);
        }
    };
    if(location != null) {
        listener.onLocation(location);
    }else{
        locationListener = new android.location.LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                listener.onLocation(location);
                locationManager.removeUpdates(locationListener);
                Log.d(TAG,"Location: " + String.valueOf(location.getLongitude()));
            }
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }
            @Override
            public void onProviderEnabled(String s) {

            }
            @Override
            public void onProviderDisabled(String s) {

            }
        };
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
Lucem
  • 2,912
  • 3
  • 20
  • 33
  • Thank you for the reply. I have added both of these to the manifest, and I did indeed allow access to location on the device. I've checked numerous times. – Dylan Woodworth Dec 04 '18 at 22:31
  • I've also tried enabling the 3 different location options on my device, "High Accuracy, Battery Saving, Phone Only" and none of them seem to change anything. I'm just really confused why it would work with the mock location app and not my real location – Dylan Woodworth Dec 04 '18 at 22:34
  • @DylanWoodworth have you tried on a different phone? – Lucem Dec 04 '18 at 22:36
  • I have not. Unfortunately both of my roommates have iPhones, I do not have access to another android currently. – Dylan Woodworth Dec 04 '18 at 22:37
  • It's working for you? Sweet, I'll take your word for it. Thanks for the replies. Odd that it isnt working on my phone... I approved the permissions. Tried uninstalling several times but no luck. – Dylan Woodworth Dec 04 '18 at 22:38
  • did you run this on your physical device or the emulator? Just want to be sure – Dylan Woodworth Dec 04 '18 at 22:44
  • yea, the app is on play store too – Lucem Dec 04 '18 at 22:45
1

I'm actually experiencing the same thing and I found a solution to make it work on the physical device. Disable the WI-FI connection and it works. Idk how or why it works though. Now I just opened back the WI-Fi connection and it's working again. If the Wi-fi thing doesn't work for you try restarting the device.

You might also need to wait for a bit before it starts getting the location while moving around the house so it constantly changes.