0

我正在做我的最后一个项目,并且是 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) {
        }
    }
}
4

1 回答 1

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
于 2012-11-11T20:09:10.573 回答