GPS提供商不返回我当前的位置android

时间:2015-02-02 04:46:08

标签: android gps geolocation locationmanager

我正在尝试获取用户当前位置,但我的onLocationChanged方法永远不会被调用。我也在Manifest.xml中添加了权限,但onLocationChange方法永远不会被调用。 我已打开我的设备的GPS并在两个不同的手机上试用但不能正常工作。 以下是我的代码 -

public class LocationAwareActivity extends Activity implements
         LocationListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocationManager locationManager = (LocationManager)
            getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 1000, 1, this);

}


@Override
public void onLocationChanged(Location location) {
    Toast.makeText(this, "Current onLocationChanged : "+location.getLatitude()+" , "+location.getLongitude(), Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(this, "onStatusChanged", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "onProviderEnabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "onProviderDisabled", Toast.LENGTH_SHORT).show();
}

及以下是manifest.xml的代码 -

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

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

TIA

3 个答案:

答案 0 :(得分:1)

您是否获得了Google地图密钥? 将它添加到您的项目中。像这样:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="Your Google Map Key" />

http://developer.android.com/google/play-services/maps.html

答案 1 :(得分:0)

Use the new Fused location provider for fetching the location.

The Fused Location Provider intelligently manages the underlying location technology and gives you the best location according to your needs.

Refer below links:
  1. https://developer.android.com/google/play-services/location.html
  2. http://javapapers.com/android/android-location-fused-provider/
  3. http://www.kpbird.com/2013/06/fused-location-provider-example.html

答案 2 :(得分:0)

由于您不移动,您可能无法获得位置更新。
尝试更改此行:

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1, this);

对此:

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, this);
相关问题