2012-07-24 156 views
0

下面的代碼中的吐司消息似乎並沒有顯示,我不能確定爲什麼。我錯過了一個錯誤嗎?GPS位置不顯示吐司

MyCurrentLocation是活動名稱

public class MyLocationListener implements LocationListener{ 
    Geocoder gc = new Geocoder(MyCurrentLocation.this.getApplicationContext(), Locale.getDefault()); 
    public void onLocationChanged(Location loc){ 

    try{ 
     loc.getLatitude(); 
     loc.getLongitude(); 
     List <Address> addr = gc.getFromLocation(loc.getLatitude(),loc.getLongitude(),1); 
     String results = ""; 
     if (addr.size() > 0){ 
     results += addr.get(0).getCountryName(); 
     } 
     Toast.makeText(getApplicationContext(),results,Toast.LENGTH_LONG).show(); 
    } catch(Exception e){ 

    } 
+1

了權限? – 2012-07-24 12:05:51

+0

它在Emulator上不適用於我。它適用於Device.Did,您可以嘗試在設備上進行測試? – Abhilasha 2012-07-24 12:07:08

+0

您是否嘗試將GPS信息發佈到Logcat? – thegrinner 2012-07-24 12:07:11

回答

0

請確保你已經在你的Android清單文件中設置正確的權限。

GPS在Android 2.x模擬器中無法正常工作。如果可能,請嘗試使用Android 4.0。

另外最好的選擇是用設備測試它,GPS也不能從封閉的位置正確工作。

你可以按照下面的鏈接 - http://www.vogella.com/articles/AndroidLocationAPI/article.html

0

嘗試使用這些權限:

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