Android LocationManager网络提供程序返回null [英] Android LocationManager network provider returns null

查看:172
本文介绍了Android LocationManager网络提供程序返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用Android App获取我的GPS坐标。我开始开发,我可以获得GPS坐标,但它们不准确。我想使用NETWORK_PROVIDER,但此提供程序的位置始终为空。更有趣的是,isProvicerEnabled返回true。



我使用了这个线程的例子(最佳答案)
在此输入链接描述

  private void _getLocation(){
//获取位置管理器
try {
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
locationManager =(LocationManager)getApplicationContext()。getSystemService(LOCATION_SERVICE);
位置位置= null;
double latitude = -1;
double longitude = -1;

//获取GPS状态
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

//获取网络状态
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
$ b $如果(!isGPSEnabled&&!isNetworkEnabled){
//没有网络提供者被启用
} else {
if(isNetworkEnabled){
showToast(网络);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
1000,
0,this);
Log.d(Network,Network Enabled);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();


$ b //如果GPS启用使用GPS服务获得经纬度
else if(isGPSEnabled){
showToast(gps );
if(location == null){
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1000,
0,this);
Log.d(GPS,GPS已启用);
if(locationManager!= null){
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!= null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
showToast(+ latitude + longitude);

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

我拥有清单中的所有权限

 <使用权限android:name =android.permission.INTERNET/> 
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>

我知道代码很脏,但现在只用于测试。我想念什么?我在很多地方发现了类似的例子,它看起来很直,所以我有点困惑。
我的手机工作正常,GPS和网络正常工作。例如,Google地图应用程序我运行良好。任何建议?

请不要使用此代码。这不好。它有很多错误。另外,如果getLastKnownLocation还没有位置,它将返回null。如果手机上没有人使用requestUpdates,它总会这样。

您的代码是从一个名为GPSTracker的非常老的线程上发布的类中提取的。我一直试图在几个月内杀死这些代码 - 这会产生比问题更多的问题。如果您想要更好的示例代码,请尝试 http://gabesechansoftware.com/location-tracking/ 这是我写的博客文章关于代码是多么糟糕。它会告诉你正确的做法,并解释一些该代码有什么问题。


I wanted to get my GPS coordinates using Android App. I started developing, and I can get GPS coordinates, but they are not accurate. I wanted to use NETWORK_PROVIDER, but the Location by this provider is always null. More interesting, isProvicerEnabled returns true.

I used example from this thread (best answer) enter link description here

private void _getLocation() {
    // Get the location manager
    try {
        boolean isGPSEnabled = false;
        boolean isNetworkEnabled = false;
        locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        Location location = null;
        double latitude = -1;
        double longitude = -1;

        // 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 {
            if (isNetworkEnabled) {
                showToast("network");
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        1000,
                        0, this);
                Log.d("Network", "Network Enabled");
                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
            else if (isGPSEnabled) {
                showToast("gps");
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            1000,
                            0, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
        showToast("" + latitude + longitude);

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

I have all the permissions in manifest

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

I know the code is dirty, but for now it's only for testing. Do I miss something? I found similar examples in many places, and it seems pretty straight, so I am a little confused. My phone works ok, GPS and network works fine. For example Google Maps application I have works well. Any suggestions?

解决方案

Please do NOT use this code. It's bad. It has a lot of errors. Also, getLastKnownLocation will return null if it doesn't have a location yet. Which it always will if nobody on the phone is using requestUpdates.

Your code is taken from a class that was posted on a very old thread on here called GPSTracker. I've been trying to kill that code for months- it causes far more problems than it helps. If you want better example code, try http://gabesechansoftware.com/location-tracking/ which is a blog post I wrote about how bad that code is. It will show you the correct way to do it, and explains some of what's wrong with that code.

这篇关于Android LocationManager网络提供程序返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆