1

我正在尝试通过 GPS 获取当前位置。但我的应用程序意外关闭。我无法得到原因。我在 AndroidManifest.xml 中添加了权限:

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

我正在尝试的代码如下:

公共类 MainActivity 扩展 Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void getLoc()
{
    LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);          
    LocationListener listener = new MyLocationListener();
    manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, listener);
}

public class MyLocationListener implements LocationListener    {
@Override

public void onLocationChanged(Location loc) 
{
    loc.getLatitude();
    loc.getLongitude();
    String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
    Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}

@Override

public void onProviderDisabled(String provider)
{
    Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}

@Override

public void onProviderEnabled(String provider)
{
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}

@Override

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

}

4

2 回答 2

1

试试这个,它可能对你有帮助

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   LocationListener mlocListener = new MyLocationListener();

 mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        mlocListener);



public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location loc) {
    loc.getLatitude();
    loc.getLongitude();

    Geocoder gcd = new Geocoder(getApplicationContext(),
            Locale.getDefault());
    try {
        mAddresses = gcd.getFromLocation(loc.getLatitude(),
                loc.getLongitude(), 1);

    } catch (IOException e) {

    }

    String cityName = (mAddresses != null) ? mAddresses.get(0)
            .getLocality() : TimeZone.getDefault().getID();
    String countryName = (mAddresses != null) ? mAddresses.get(0)
            .getCountryName() : Locale.getDefault().getDisplayCountry()
            .toString();


    mCurrentSpeed.setText(loc.getSpeed());
}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(), "Gps Disabled",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(getApplicationContext(), "Gps Enabled",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
于 2012-11-24T19:12:30.437 回答
1

尝试这个。首先使这段代码简单。

在 onCreate 中添加这一行,因为我没有看到在 onCreate 中调用的任何 getloc 方法

LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);          
    LocationListener listener = new MyLocationListener();
    manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 5000, 10, listener);

并为了测试将这些值更改为 0

manager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, listener);

并使用此方法更改 onLocationChange。

public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
  }

或试试这个链接

于 2012-11-24T17:30:49.673 回答