【问题标题】:Android : Can't get location by WIFI on android TVAndroid:无法在 android TV 上通过 WIFI 获取位置
【发布时间】:2017-06-09 12:11:00
【问题描述】:

这是我的问题:

我有这个代码(来自这个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;
}

它在 AVD 和手机上工作得很好,但在 android TV 上就不行了。

我的 android TV 正在“使用 Wifi 估计位置”(当然是“开启”)所以我认为即使没有 GPS_PROVIDER,它至少应该在 NETWORK_PROVIDER 上运行,但是 isNetworkEnabled = locationManager.isProviderEnabled (LocationManager.NETWORK_PROVIDER); 总是错误的。

我指定我的电视上有 wifi,当我在浏览器中输入谷歌地图时,它找到了我。

谁能帮我理解为什么它不起作用或提出其他解决方案来获取我的位置(除了谷歌播放服务,我的电视没有安装它)...

编辑

我的清单:

<!-- 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" />

【问题讨论】:

  • 你的安卓版本是多少?如果是 Marshmellow(6.x),你是否获得了运行时权限?
  • @dustblue 我的最小 SDK 为 21,最大为 25,我的电视使用 API 22,是的,我有运行时权限
  • @dustblue 如果你愿意,我把我的清单放在问题中

标签: android location android-wifi android-tv


【解决方案1】:

您可以使用 Google Play 服务中的LocationServices API 来获取用户的位置。它是一个“FusedLocationProvider”,结合了包括 Wi-Fi 在内的各种输入来确定用户的位置。

首先:添加COARSE_LOCATION权限:

&lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;

然后初始化你的对象:

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

并通过监听器获取用户的最后位置

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) {
                // ...
            }
        }
    });

【讨论】:

    【解决方案2】:

    您可以参考此thread,其中声明您只需要在您的清单中获得这些权限:

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

    然后替换条件以检查您是否启用了 GPS:

    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
    

    其他参考资料:

    希望这会有所帮助!

    【讨论】:

    • 抱歉,回答迟了,您给我的代码是我已经使用的代码,并且我已经拥有权限。麻烦的是部分:一些内部状态已准备好进行网络定位,我不知道我的状态是否已准备好:/ 我试图把 isNetworkEnabled = locationManager. isProviderEnabled(LocationManager.NETWORK_PROVIDER); 为真,但当我这样做时,我的位置为空......
    猜你喜欢
    • 2013-09-20
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多