我有一个以吐司的形式显示GPS坐标的应用程序,但我想要实现的是保存这些坐标,并在下一个活动中显示它们。问题是..我不能!过去4天我一直在研究这个问题,但我不知道该怎么做。如果有人有任何建议我会非常感激。下面看看代码:
public class GPSActivity extends Activity{
double longitude = 0;
double latitude = 0;
String s1 = "latitude";
String s2 = "longitude";
public Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intent = new Intent(this, Display.class);
/* Use the LocationManager class to obtain GPS locations */
LocationManager myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener myLocationListener = new MyLocationListener();
myLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);} // end activity
/* Class My Location Listener */
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
longitude = loc.getLatitude();
latitude = loc.getLongitude();
String text = "My current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), text, Toast.LENGTH_SHORT).show();
Bundle extras = new Bundle();
extras.putDouble("long", longitude);
extras.putDouble("lat", latitude);
intent.putExtras(extras);
startActivity(intent); }
@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
答案 0 :(得分:0)
我不会将Intent存储为成员变量。在想要启动它的位置创建它。
Bundle extras = new Bundle();
extras.putDouble("long", longitude);
extras.putDouble("lat", latitude);
Intent newIntent = new Intent(GPSActivity.this, Display.class);
newIntent.putExtras(extras);
startActivity(newIntent);
还要在AndroidManifest.xml中定义您的展示活动
<manifest . . . >
. . .
<application . . .>
<activity android:name=".Display"
. . . >
. . .
</activity>
</application>
</manifest>
答案 1 :(得分:0)
也许你应该创建一个像这样的课程
String lon; //variable Lon to pass to another activity
String lat; //variable Lat to pass to another activity
public void ALoc(double lon, double lat){
this.lon = String.valueOf(lon);
this.lat = String.valueOf(lat);
}
然后你把它放在你的onLocationChanged()或者把最近的坐标传递给上面声明的String Lon和Lat。
Location lokasiku = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//...
//blabla code
//...
ALoc(lokasiku.getLongitude(), lokasiku.getLatitude());
在按钮中转到使用此参数的下一个活动,您应该通过onClick()或者什么来通过意图传递参数。假设你在布局上有一个带有id:button_Go的GoButton。
GoButton = (Button) findViewById(R.id.button_Go);
GoButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecentActivity.this, NextActivity.class);
intent.putExtra("lon", lon);
intent.putExtra("lat", lat);
RecentActivity.this.startActivity(intent);
}
});
现在,在NextActivity上,你应该得到我们传递的数据,如上所述,看起来像这样,把它放在onCreate()上。
String lon = getIntent().getExtras().getString("lon");
String lat = getIntent().getExtras().getString("lat");
希望这会有所帮助,至少它已经对我有用了。 :d