2011-11-29 100 views
1

我試圖在我的模擬器上用telnet和使用DDMS透視圖來設置GPS位置。似乎沒有什麼工作,因爲如果你認爲我的代碼在屏幕上的輸出將是「無法獲得GPS位置」。當我在手機上運行應用程序時,該應用程序可以工作代碼:在Android模擬器上設置GPS位置

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tvLat = (TextView) findViewById(R.id.tvLat); 
    tvLongi = (TextView) findViewById(R.id.tvLongi); 
    tvLat.setText("test"); 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria crit = new Criteria(); 
    providers = lm.getBestProvider(crit, false); 
    Location loc = lm.getLastKnownLocation(providers); 
    if (loc != null) { 
     x = loc.getLatitude(); 
     y = loc.getLongitude(); 

     t = "Current latitude: " + x; 
     t1 = "Current longitude: " + y; 
     tvLat.setText(t); 
     tvLongi.setText(t1); 
    } else { 
     tvLat.setText("Couldn't get a GPS location"); 
    } 

} 

@Override 
public void onLocationChanged(Location loc) { 
    // TODO Auto-generated method stub 
    x = loc.getLatitude(); 
    y = loc.getLongitude(); 
    t = "Current latitude: " + x; 
    t1 = "Current longitude: " + y; 

    tvLat.setText(t); 
    tvLongi.setText(t1); 

} 

@Override 
public void onProviderDisabled(String arg0) { 
    // TODO Auto-generated method stub 
    tvLat.setText("GPS disabled"); 
} 

@Override 
public void onProviderEnabled(String arg0) { 

    tvLat.setText("GPS enabled"); 
} 

@Override 
public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 


} 
Permissions: 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> 
+0

可能重複[如何在Android模擬器模擬GPS位置?(http://stackoverflow.com/questions/2279647/how-to-emulate -gps-location-in-the-android-emulator) –

回答

3

DDMS Perspective從位於Eclipse中的左下方你有Locations ControlsEmulator Control標籤。從那裏您可以輸入經度和緯度,然後單擊發送將這些座標發送到選定的模擬器。

也許你仍然無法獲取上次知道的位置,但這會觸發onLocationChanged()方法。

編輯:我看到你的班級實施LocationListener。您是否使用lm.requestLocationUpdates()註冊了班級的位置更改?

EDIT2:我的意思是:

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this); 
lm.requestLocationUpdates(LocationManager.GPS, 0, 0,this); 
+0

正如我所說,我已經嘗試了DDMS的角度,但我沒有得到任何不同的輸出在我的屏幕上。 – Carlj901

+0

「註冊你的課程」是什麼意思? – Carlj901

+0

如果我添加:\t \t \t \t lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);和lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);輸出將在屏幕上的「GPS禁用」,如果我嘗試設置一個位置使用DDMS它說:「無法發送命令模擬器」 – Carlj901