3
我使用此代碼來獲取我的位置並在屏幕上打印座標。 幫助使用GPS座標,Android
package com.example.alpha;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class alpha extends Activity
{
private LocationManager lm;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
double lat = loc.getLatitude();
double lon = loc.getLongitude();
Toast.makeText(getBaseContext(), "Location changed : alphaLat: " + lat +
" alphaLng: " + lon,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}
}
的問題是,我想使用的緯度和經度值,以便通過流送他們。有關如何獲取onLocationChanged方法外部座標的任何建議?
那麼他們是不是仍然會在方法中取得它們的價值,並且價值不會超越它? – kotsosh 2011-03-21 23:14:25
@ kotsosh不理解你的問題。如果您想訪問該類之外的變量,請考慮使用sharedpreferences或單例對象。我使用單例對象在我的服務和活動之間傳遞值。 – GSree 2011-03-22 00:02:31