Android:在Android TV上无法通过WIFI获取位置

时间:2017-06-09 12:11:01

标签: android location android-wifi android-tv

这是我的问题:

我有这段代码(来自此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电视正在“使用Wifi估算位置”(当然是“开启”)所以我认为即使没有GPS_PROVIDER,它至少应该继续使用NETWORK_PROVIDER,但 isNetworkEnabled = locationManager.isProviderEnabled (LocationManager.NETWORK_PROVIDER); 始终为false。

我指定我的电视上有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" />

2 个答案:

答案 0 :(得分:0)

您可以参考此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

其他参考资料:

希望这有帮助!

答案 1 :(得分:0)

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

首先:添加trigger('myAwesomeAnimation', [ transition(':enter', [ query(':leave', [ style({transform: 'scale(0.8)',}), stagger(100, [ animate('1.5s ease-out', style({transform: 'scale(1)',})), ]) ]), ... ]), ]) 权限:

COARSE_LOCATION

然后初始化你的对象:

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

通过听众获取用户的最后位置

FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);