0

我想获取 Android 移动位置。所以我需要得到LocationManager,但是发生了异常,我不知道为什么。你能帮助我吗?下面是我的代码。

public static String getMobileLocation(Context context) {
    LocationManager lm = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);

    if (null == lm) {
        return "error01";
    }
    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
    lm.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = lm.getBestProvider(criteria, true);
    if (provider == null || provider.length() == 0) {
        return "error02";
    }

    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            LocationInfo.location = location;
        }
    };

    lm.requestLocationUpdates(provider, 0, 0, locationListener);

    while (null == location) {
        location = lm.getLastKnownLocation(provider);
    }
    if (null == location) {
        return "error03";
    }
    StringBuilder strBuilder = new StringBuilder();
    strBuilder.append(location.getLatitude());
    strBuilder.append(",");
    strBuilder.append(location.getLongitude());
    String str = strBuilder.toString();
    str = str.trim();
    if (null == str || str.length() == 0) {
        return "error04";
    }

    lm.removeUpdates(locationListener);
    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, false);
    lm.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, false);
    location = null;

    return str;
}

当我得到LocationManager. DDMS 日志在这里。引起:java.lang.IllegalArgumentException:在 android.os.Parcel.readException(Parcel.java:1331) 在 android.os.Parcel.readException(Parcel.java:1281) 在 android.location.ILocationManager 提供者“gps”未知$Stub$Proxy.setTestProviderEnabled(ILocationManager.java:1122) 在 android.location.LocationManager.setTestProviderEnabled(LocationManager.java:1140) 在*.record.LocationInfo.getMobileLocation(LocationInfo.java:35) 在.MainActivity.initLocation(MainActivity.java:45) 在 * *.MainActivity.onCreate(MainActivity.java:40)

4

1 回答 1

2

更新:

LocationManager.GPS_PROVIDER不适用于setTestProviderEnabled()

它似乎在某种程度上被弃用了。

利用:

locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);
于 2013-02-21T15:56:45.213 回答