2017-08-30 61 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