GPS位置提供商在Android

时间:2016-02-04 06:06:21

标签: android gps location locationmanager locationlistener

我尝试在android中使用GPS_Provider查找位置。当尝试使用Network_Provider获取位置时,它会给我定位并正确调用回调函数,但当我尝试使用GPS_Provider获取位置时,它会给我位置 null &也没有调用onLocationChanged的回调函数LocationListener

我的代码是:

 LocationManager lm;
    boolean isNetwork = false;
    Location location;
    LocationListener locationListener;
    LocationListener loc;
    MyLocation myLocation;
    Location location1;

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

        lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        try {
            myLocation = new MyLocation();

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                Log.e("Location", "Permission Not Given");
                return;
            }
            Log.e("Location", "Permission is Given");
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocation);
            location1=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }catch(Exception e)
        {

        }
    }

MyLocation.class

public class MyLocation implements LocationListener {
    @Override
    public void onLocationChanged(Location location) {

        if(location!=null)
        {

            String lat,Longi;
            lat=String.valueOf(location.getLatitude());
            Longi=String.valueOf(location.getLongitude());

            Log.e("Latitude: " , lat);
            Log.e("Longitude: " , Longi);

        }
        else
            Log.e("Location is null", "Location not Found");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e("Latitude: " ,""+ provider+ " Status: "+status);
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e("Latitude: " , provider);
    }
}

它给我发出logcat错误如下:

02-04 00:50:52.280 14480-14480/com.arc.locationusingnetwork E/Location: Permission is Given
02-04 00:50:52.360 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.390 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52

3 个答案:

答案 0 :(得分:2)

旧的LocationListener类可能已弃用。您应该使用Android的FusedLocationApi来获取当前位置或通过FusedLocationAPI的LocationListener接收位置更新。

这是使用FusedLocationApi的好教程:Android Location API

答案 1 :(得分:0)

您需要移动设备以致电onLocationChanged LocationListener LocationListener用于在位置发生变化时从LocationManager接收通知。如果已使用LocationListener方法向位置管理器服务注册了LocationManager.requestLocationUpdates(String, long, float, LocationListener),则会调用这些方法。

现在无法保证getLastKnownLocation始终返回Location,因为它会返回上一个位置,但如果新安装了应用或用户确实清除了数据,那么gps上一个已知位置会返回null所以处理这个你应该移动你的设备,以便调用onLocationChanged(我建议你打开以便设备快速连接到gps)。

您可以使用FusedLocationProviderApi,因为它是最新的API ,是获取Android位置的最佳选择。 google docs : Getting the Last Known Location

答案 2 :(得分:0)

您是否检查过您的GNSS / GPS是否已启用?

boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);