2013-02-05 44 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

相關問題