2013-02-27 32 views
0

我只想看看從粗略的位置修復中返回位置的格式。使用谷歌代碼,如下所示:簡單的網絡位置測試不起作用?

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_get_mission); 


    // Acquire a reference to the system Location Manager 
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     // Define a listener that responds to location updates 
     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       // Called when a new location is found by the network location provider. 
       makeUseOfNewLocation(location); 
      } 


      public void onStatusChanged(String provider, int status, Bundle extras) {} 

      public void onProviderEnabled(String provider) {} 

      public void onProviderDisabled(String provider) {} 
      }; 

     // Register the listener with the Location Manager to receive location updates 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    } 


    private void makeUseOfNewLocation(Location location) { 

     TextView locationOutput = (TextView)findViewById(R.id.locationOutput); 
     locationOutput.setText((CharSequence) location); 

    } 

這不適用於我的手機,所以我認爲這並不容易。我錯過了什麼?

TIA

當前權限啓用:

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

您是否編輯過Manifest.xml – 2013-02-27 14:52:29

+3

您是否在設備的設置中啓用了無線網絡和網絡位置? – ebarrenechea 2013-02-27 14:53:32

+0

是的,我已將COARSE位置添加到清單。 – 2013-02-27 15:03:12

回答

1

你在清單中添加的權限?

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

此外,您可以獲得空位置,確保能夠處理它。

編輯

嘗試getLastKnownLocation(LocationManager.GPS_PROVIDER);或增加許可的WiFi。

+0

我已經允許粗糙但不好。 – 2013-02-27 15:02:24