我正在研究我的最终项目并且是Android新手。我想将速度从LocationManager传递给Activity,我试图用Intent传递它,
我的意思是(Intent j = new Intent(this, CallblockingActivity.class)
),但它不起作用。请帮帮我。
public class ShowLocationActivity extends Activity {
private TextView latituteField;
private TextView longitudeField;
private TextView speed;
public float a;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmanager);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
speed = (TextView)findViewById(R.id.textspeed);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
float lat = (float) (location.getLatitude());
float lng = (float) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
a=location.getSpeed();
speed.setText("current speed" + a);
*** **My problwm is here,I just want to pass a**
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
Toast.makeText(ShowLocationActivity.this,
location.getLatitude() + "" + location.getLongitude(),
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
答案 0 :(得分:1)
只需使用Intent#putExtra()
:
Intent j = new Intent(ShowLocationActivity.this, CallblockingActivity.class);
j.putExtra("Speed", a);
startActivity(j);
在你的新活动中,阅读速度:
Intent intent = getIntent();
float speed = intent.getFloatExtra("Speed", 0); // 0 is the default case