2017-08-30 59 views
-2

我想檢測我的位置,我使用ACCESS_FINE_LOCATION許可,但是當我運行的應用程序,它崩潰並在屏幕上出現錯誤信息:如何在android中使用GPS獲取位置?

「APPNAME」已停止工作

以下是我的代碼,我用於實現上述說明:

public class MainActivity extends AppCompatActivity { 
    LocationManager locationManager; 
    Location location; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     locationManager = (LocationManager) 
         getSystemService(Context.LOCATION_SERVICE); 
     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     Log.v("My Coordinates are: ", location.getLatitude() + " " + location.getLongitude()); 
    } 
} 

我在做什麼錯在這裏?

請幫忙。

+0

檢查此[Android的GPS定位管理器(https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/) –

+0

如果您使用的是Android 6 +,那麼這可能是由於運行時權限造成的。您可以閱讀接受的答案在https://stackoverflow.com/questions/40142331/how-to-request-location-permission-on-android-6。另外,如果你可以提供你的堆棧跟蹤會更容易。 – BajajG

+0

logcat中會有堆棧跟蹤,它會告訴你更多關於這個問題的信息。檢查https://stackoverflow.com/questions/23353173/unrible-myapp-has-stopped-how-can-i-solve-this/23353174#23353174 –

回答

0

把您的許可,在清單中,所以你不必代碼你再在其他活動中沒有

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

實例化外景經理:如果GPS

LocationManager locationManager = (LocationManager) 
getSystemService(Context.LOCATION_SERVICE); 

獲取座標啓用:

LocationListener locationListener = new MyLocationListener(); 
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 

這是一個完整實現的示例:

private class MyLocationListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location loc) { 
     editLocation.setText(""); 
     pb.setVisibility(View.INVISIBLE); 
     Toast.makeText(
       getBaseContext(), 
       "Location changed: Lat: " + loc.getLatitude() + " Lng: " 
        + loc.getLongitude(), Toast.LENGTH_SHORT).show(); 
     String longitude = "Longitude: " + loc.getLongitude(); 
     Log.v(TAG, longitude); 
     String latitude = "Latitude: " + loc.getLatitude(); 
     Log.v(TAG, latitude); 

     /*------- To get city name from coordinates -------- */ 
     String cityName = null; 
     Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); 
     List<Address> addresses; 
     try { 
      addresses = gcd.getFromLocation(loc.getLatitude(), 
        loc.getLongitude(), 1); 
      if (addresses.size() > 0) { 
       System.out.println(addresses.get(0).getLocality()); 
       cityName = addresses.get(0).getLocality(); 
      } 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String s = longitude + "\n" + latitude + "\n\nMy Current City is: " 
      + cityName; 
     editLocation.setText(s); 
    } 

    @Override 
    public void onProviderDisabled(String provider) {} 

    @Override 
    public void onProviderEnabled(String provider) {} 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
} 
+0

感謝您的幫助,但是位置移動員在您移動時更新座標,並且即使停止,我也想每次都獲取我的位置。 – Mathew

+0

是的,您可以使用從'onLocationChanged'收集的數據在您每次需要時獲取您的位置。 – Orvenito