在三星Galaxy Grand android的当前位置获取null?

时间:2013-06-14 12:47:57

标签: android android-maps-v2

我尝试在三星Galaxy Grand上运行我的应用程序,但它没有显示我当前的位置。

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    System.out.println("provider => " + provider);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);
    System.out.println("location  " + location);

但总是这一行返回null locationManager.getLastKnownLocation(provider); 但是这个应用程序在 sony和lg 等其他手机中运行良好。

我尝试了很多链接,但仍然存在同样的问题。

getLastKnownLocation() always returning null, all providers are DummyLocationProvider

Android LocationManager.getLastKnownLocation() returns null

Network provider and GPS provider returning null values

许可

<permission
    android:name="xx.xxx.xxx.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="xicom.biz.perdiem.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />


<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application android:label="@string/app_name" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyccccccccbvxcbpmFLAZwalBGM" />
......

1 个答案:

答案 0 :(得分:1)

检查this以获取答案,简而言之

要以编程方式启动“当前位置”的应用,请在致电getLastKnownLocation之前运行以下代码

YOUR_APPLICATION_CONTEXT.getLocationManager().requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onLocationChanged(final Location location) {
    }
});

修复GPSTracker问题的一些建议可能是,将其设置为1分1米

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 ;