2013-02-05 45 views
4

我有这个令人毛骨悚然的问题。我正在尝试获取我的模拟器的位置。它工作的很好,当我得到我的模拟器的位置。但是当我改变我的位置坐标没有任何反应。 gps工作正常。LocationManager requestLocationUpdates不工作

这里是我的代码

Main.java

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

import android.util.Log; 
import android.view.Menu; 
import android.widget.TextView; 

public class Main extends Activity { 
TextView tvStatus; 
LocationManager lm; 
boolean gpsEnabled; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    tvStatus = (TextView) findViewById(R.id.textView1); 

    lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     Log.d("", "GPS is Active"); 
     Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     tvStatus.setText(l.getLatitude()+" , "+l.getLongitude()); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, 
       new LocationListener() { 

        @Override 
        public void onStatusChanged(String arg0, int arg1, 
          Bundle arg2) { 
         // TODO Auto-generated method stub 

        } 

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

        } 

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

        } 

        @Override 
        public void onLocationChanged(Location arg0) { 
         tvStatus.setText("GPS ENABLED: " + gpsEnabled 
           + "\nLatitude: " + arg0.getLatitude() 
           + "\nLongitude: " + arg0.getLongitude()); 
        } 
       }); 
    } 

} 

    } 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.enabelinglocationprovider" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="5" 
     android:targetSdkVersion="10" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.enabelinglocationprovider.Main" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

是位置相关的服务在该设备中打开? – Pinki

+0

Ya,gps已启用。还有一件事。我一直在eclipse中切换工作区。现在当我重新启动我的模拟器时,它工作正常。 –

+0

当我切换我的工作区时,模拟器断开了连接? –

回答

0

它发生与仿真器,尝试重新启动模拟器。

或者关闭Eclipse和仿真器和启动这两个再次

3

三星手机都有这个问题。这有点破解。您需要启动位置管理器以获取地图缓存中的最新位置。

God.locationManager.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) { 
       } 
      }); 
    currentLocation = God.locationManager 
      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

呼叫getLastKnownLocation后的0踢requestLocationUpdates

+0

我也必须为5.1.1 Android版本的Nexus 5做到这一点 – gilsaints88

相关问题