1
我試圖創建一個簡單的應用程序,從而GPS座標將被存儲,並且只顯示在下一個活動的屏幕上。Android:如何保存GPS座標,並在找到時開始新的活動?
到目前爲止,我已經設法讓座標顯示在吐司上,所以我知道代碼正在工作。我在邏輯上遇到問題。當我找到經度和緯度時,我試圖開始一個新的活動(一個將顯示座標)。
任何人都可以提供任何建議,如何我可以做這樣的事情?
public class GPSActivity extends Activity{
float longitude = 0;
float latitude = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
Intent intent = new Intent(this, Display.class);
Bundle extras = new Bundle();
do{
extras.putFloat("long", longitude);
extras.putFloat("lat", latitude);
}while(longitude == 0);
// if longitude is no longer zero - meaning it is found
// add extras and call activity.
if(longitude != 0){
intent.putExtras(extras);
startActivity(intent);
}} // end activity
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
longitude = (float) loc.getLatitude();
latitude = (float) loc.getLongitude();
String text = "My current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + 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){
}
}} // End GPS activity
嗨,我已經試過這種方法,但應用程序崩潰時試圖啓動活動,引發nullpointerexception。 – BodhiByte 2012-02-01 22:12:35
是的,這個例子假設你想要啓動的活動是Display.class,也許情況並非如此,對吧? – Scalarr 2012-02-02 23:47:34
否顯示類將顯示已顯示的座標 – BodhiByte 2012-02-03 00:39:12