0

here's my problem :

I have this code (from this tuto) :

public Location getLocation(Context act) {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (ContextCompat.checkSelfPermission(act, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    // First get location from Network Provider
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                // if GPS Enabled get lat/long using GPS Services

                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

It works just fine on AVD and phone but with android TV, it won't work.

My android TV is on "use Wifi to estimate location" ( and is "on" of course) so I thought that even if there is no GPS_PROVIDER it at least should go on NETWORK_PROVIDER but isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); is always false.

I specify that I have wifi on my tv and when I entered google map in my browser it found me.

Can someone please help me understand why it doesn't work or propose an other solution to get my location (apart from google play service, my TV doesn't have it installed)...

EDIT

My manifest :

<!-- lire dans la carte sd -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- écrire dans la carte sd -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- accès à internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- accès à l'état de la connection internet -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- pour enlever le bouton recent app -->
<uses-permission android:name="android.permission.REORDER_TASKS" />
<!-- pour l'AccountManager -->
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<!-- pareil je crois -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Pour delete les accounts -->
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

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

<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />
<uses-feature
    android:name="android.software.leanback"
    android:required="true" />
<uses-feature
    android:name="android.hardware.faketouch"
    android:required="false" />
<uses-feature
    android:name="android.hardware.telephony"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="false" />
<uses-feature
    android:name="android.hardware.location.gps"
    android:required="false" />

<uses-feature android:name="android.hardware.location.network"/>

<uses-feature
    android:name="android.hardware.microphone"
    android:required="false" />
<uses-feature
    android:name="android.hardware.sensor"
    android:required="false" />

<supports-screens
    android:largeScreens="true"
    android:normalScreens="false"
    android:requiresSmallestWidthDp="720"
    android:smallScreens="false"
    android:xlargeScreens="true" />
Kolopox
  • 276
  • 1
  • 4
  • 17

2 Answers2

1

You can use the LocationServices API in Google Play Services to obtain the user's location. It is a "FusedLocationProvider", incorporating a variety of inputs including Wi-Fi to determine a user's location.

First: Add the COARSE_LOCATION permission:

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

Then init your object:

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

And get the user's last location through a listener

mFusedLocationClient.getLastLocation()
    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                // ...
            }
        }
    });
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
0

You may refer with this thread wherein it stated that you will only need these permissions in you manifest:

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

Then replace the condition to check if you GPS is enabled:

if (isGPSEnabled) {
   if (location == null) {
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener)this);
       Log.d("GPS Enabled", "GPS Enabled");
       if (locationManager != null) {
           location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
           if (location != null) {
               latitude = location.getLatitude();
               longitude = location.getLongitude();
           }
       }
   }
} //end-if isGPSEnabled

Additional references:

Hope this helps!

abielita
  • 13,147
  • 2
  • 17
  • 59
  • Sorry for the late answer, the code you gave me is the one I already use et I already have the permissions. The thing that bother is the part : **Some internal state is ready for network location**, I don't know if my state is ready or not :/ and I tried to put **isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);** to true but when I do my Location is null... – Kolopox Jun 16 '17 at 14:35