如何将数据从一个活动转移到另一个活动? Android应用程序

时间:2014-06-29 15:53:01

标签: android

我想将数据(经度和纬度)从一个活动转移到另一个活动。目标是将这些数据传输到相机,以查看对象(增强现实)如何实时移动。

LocationGoogleMap.class

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.beyondar.android.plugin.googlemap.GoogleMapWorldPlugin;
import com.beyondar.android.world.GeoObject;
import com.beyondar.android.world.World;
import com.example.pfg_v7.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;


public class LocationGoogleMap extends FragmentActivity implements
    OnMarkerClickListener, OnClickListener, LocationListener {

private GoogleMap mMap;
private GoogleMapWorldPlugin mGoogleMapPlugin;
private World mWorld;
Marker mMark;

private LocationManager oLoc;
private Location currentLoc;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_google);

    Button myLocationButton = (Button) findViewById(R.id.myLocationButton);
    myLocationButton.setVisibility(View.VISIBLE);
    myLocationButton.setOnClickListener(this);

    Criteria oGPSSettings = new Criteria();
    oGPSSettings.setAccuracy(Criteria.ACCURACY_FINE);
    oGPSSettings.setSpeedRequired(true);
    oGPSSettings.setAltitudeRequired(true);
    oGPSSettings.setBearingRequired(true);
    oGPSSettings.setCostAllowed(false);
    oGPSSettings.setPowerRequirement(Criteria.POWER_MEDIUM);

    oLoc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    String provider = oLoc.getBestProvider(oGPSSettings, true);

    if (provider != null) {
        oLoc.requestLocationUpdates(provider, 1000, 1, this);
    } else {
        Toast.makeText(getBaseContext(), "No GPS", Toast.LENGTH_SHORT)
                .show();
        finish();
    }

    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
    if (mMap == null) {
        return;
    }

    // We create the world and fill the world
    mWorld = CreateWorld.generateObjects(this);

    // As we want to use GoogleMaps, we are going to create the plugin and
    // attach it to the World
    mGoogleMapPlugin = new GoogleMapWorldPlugin(this);
    // Then we need to set the map into the GoogleMapPlugin
    mGoogleMapPlugin.setGoogleMap(mMap);
    // Now that we have the plugin created let's add it to our world.
    // NOTE: It is better to load the plugins before start adding object in
    // to the world.
    mWorld.addPlugin(mGoogleMapPlugin);

    mMap.setOnMarkerClickListener(this);

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mGoogleMapPlugin.getLatLng(), 15));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(19), 2000, null);

}

@Override
public void onLocationChanged(Location location) {

    currentLoc = location;

    LatLng oPos = new LatLng(currentLoc.getLatitude(),
            currentLoc.getLongitude());

    mMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    if (mMap != null) {
        mMap.clear();

        mMark = mMap.addMarker(new MarkerOptions().position(oPos).title(
                "My Location"));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(oPos, 17));

    }

    // We create the world and fill the world
    mWorld = CreateWorld.generateObjects(this);

    // As we want to use GoogleMaps, we are going to create the plugin and
    // attach it to the World
    mGoogleMapPlugin = new GoogleMapWorldPlugin(this);
    // Then we need to set the map into the GoogleMapPlugin
    mGoogleMapPlugin.setGoogleMap(mMap);
    // Now that we have the plugin created let's add it to our world.
    // NOTE: It is better to load the plugins before start adding object in
    // to the world.
    mWorld.addPlugin(mGoogleMapPlugin);



}



/*
 * 
 * mMap = ((SupportMapFragment)
 * getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); if
 * (mMap == null) { return; }
 * 
 * // We create the world and fill the world mWorld =
 * CreateWorld.generateObjects(this);
 * 
 * 
 * 
 * // As we want to use GoogleMaps, we are going to create the plugin and //
 * attach it to the World mGoogleMapPlugin = new GoogleMapWorldPlugin(this);
 * // Then we need to set the map into the GoogleMapPlugin
 * mGoogleMapPlugin.setGoogleMap(mMap); // Now that we have the plugin
 * created let's add it to our world. // NOTE: It is better to load the
 * plugins before start adding object in // to the world.
 * mWorld.addPlugin(mGoogleMapPlugin);
 * 
 * //NO
 * AÑADIDO//////----------------------------------------////////////////
 * ////////////////////////////////////////
 * mMap.setOnMarkerClickListener(this);
 * 
 * mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mGoogleMapPlugin.getLatLng
 * (), 15)); mMap.animateCamera(CameraUpdateFactory.zoomTo(19), 2000, null);
 * //NO
 * AÑADIDO/////-----------------------------------------////////////////
 * /////////////////////////////////////////
 * 
 * // Lets add the user position to the map GeoObject user = new
 * GeoObject(1000l); user.setGeoPosition(mWorld.getLatitude(),
 * mWorld.getLongitude()); user.setImageResource(R.drawable.flag);
 * user.setName("User position"); mWorld.addBeyondarObject(user);
 * 
 * BeyondarLocationManager.addWorldLocationUpdate(mWorld);
 * BeyondarLocationManager.addGeoObjectLocationUpdate(user);
 * 
 * // We need to set the LocationManager to the BeyondarLocationManager.
 * BeyondarLocationManager .setLocationManager((LocationManager)
 * getSystemService(Context.LOCATION_SERVICE));
 */

@Override
public boolean onMarkerClick(Marker marker) {
    // To get the GeoObject that owns the marker we use the following
    // method:
    GeoObject geoObject = mGoogleMapPlugin.getGeoObjectOwner(marker);
    if (geoObject != null) {
        Toast.makeText(
                this,
                "Click on a marker owned by a GeoOject with the name: "
                        + geoObject.getName(), Toast.LENGTH_SHORT).show();
    }
    return false;
}

@Override
protected void onResume() {
    super.onResume();
    // When the activity is resumed it is time to enable the
    // BeyondarLocationManager
    oLoc.removeUpdates(this);
    oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    // To avoid unnecessary battery usage disable BeyondarLocationManager
    // when the activity goes on pause.
    oLoc.removeUpdates(this);
}

@Override
public void onClick(View v) {
    // When the user clicks on the button we animate the map to the user
    // location
    LatLng userLocation = new LatLng(currentLoc.getLatitude(),
            currentLoc.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(19), 2000, null);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    oLoc.removeUpdates(this);
    oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);

}

@Override
public void onProviderEnabled(String provider) {
    oLoc.removeUpdates(this);
    oLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
    Toast.makeText(getBaseContext(), provider + " is enabled.",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderDisabled(String provider) {
    oLoc.removeUpdates(this);
    Toast.makeText(getBaseContext(), provider + " is disabled.",
            Toast.LENGTH_SHORT).show();

}

}

CameraWithLocation.class

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.beyondar.android.fragment.BeyondarFragmentSupport;
import com.beyondar.android.world.World;

public class CameraWithLocation extends FragmentActivity implements
    OnSeekBarChangeListener, OnClickListener {

private BeyondarFragmentSupport mBeyondarFragment;
private World mWorld;

private SeekBar mSeekBarMax, mSeekBarMin;
private Button mShowMap;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Hide the window title.
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    loadViewFromXML();


    // We create the world and fill it ...
    mWorld = CreateWorld.generateObjects(this);
    // .. and send it to the fragment
    mBeyondarFragment.setWorld(mWorld);

    // We also can see the Frames per seconds
    mBeyondarFragment.showFPS(true);

}

private void loadViewFromXML() {

    setContentView(R.layout.camera_with_location);

    mBeyondarFragment = (BeyondarFragmentSupport) getSupportFragmentManager()
            .findFragmentById(R.id.beyondarFragment);

    mSeekBarMax = (SeekBar) findViewById(R.id.seekBarMax);
    mSeekBarMin = (SeekBar) findViewById(R.id.seekBarMin);
    mSeekBarMax.setOnSeekBarChangeListener(this);
    mSeekBarMin.setOnSeekBarChangeListener(this);
    mSeekBarMax.setMax(100);
    mSeekBarMin.setMax(100);

    mShowMap = (Button) findViewById(R.id.showMapButton);
    mShowMap.setOnClickListener(this);
}



@Override
public void onClick(View v) {
    if (v == mShowMap) {
        Intent intent = new Intent(this, LocationGoogleMap.class);
        startActivity(intent);
    }
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    if (seekBar == mSeekBarMax) {
        mBeyondarFragment.setMaxFarDistance(progress);
    } else if (seekBar == mSeekBarMin) {
        mBeyondarFragment.setMinFarDistanceSize(progress);
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

}

CreateWorld.class

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.beyondar.android.plugin.googlemap.GoogleMapWorldPlugin;
import com.beyondar.android.world.GeoObject;
import com.beyondar.android.world.World;
import com.example.pfg_v7.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class CreateWorld{

public static final int LIST_TYPE_EXAMPLE_1 = 1;


public static World sharedWorld;


public static World generateObjects(Context context) {
    if (sharedWorld != null) {
        return sharedWorld;
    }

    sharedWorld = new World(context);

    // The user can set the default bitmap. This is useful if you are
    // loading images form Internet and the connection get lost
    sharedWorld.setDefaultImage(R.drawable.beyondar_default_unknow_icon);

    // User position (you can change it using the GPS listeners form Android
    // API)



    sharedWorld.setGeoPosition(40.332177, -3.766126);

    // Create an object with an image in the app resources.
    GeoObject go1 = new GeoObject(1l);
    go1.setGeoPosition(40.390691, -3.636787);
    go1.setImageResource(R.drawable.rectangle);
    go1.setName("CercaDeCasa");

    GeoObject go2 = new GeoObject(2l);
    go2.setGeoPosition(40.391076, -3.635275);
    go2.setImageResource(R.drawable.rectangle);
    go2.setName("Cruce Superior");

    GeoObject go3 = new GeoObject(3l);
    go3.setGeoPosition(40.390821, -3.635967);
    go3.setImageResource(R.drawable.rectangle);
    go3.setName("Cruce del parque");

    GeoObject go4 = new GeoObject(4l);
    go4.setGeoPosition(40.390288, -3.635356);
    go4.setImageResource(R.drawable.rectangle);
    go4.setName("Callejuela");





    // Add the GeoObjects to the world
    sharedWorld.addBeyondarObject(go1);
    sharedWorld.addBeyondarObject(go2);
    sharedWorld.addBeyondarObject(go3);
    sharedWorld.addBeyondarObject(go4);


    return sharedWorld;
}

}

总结一下:我需要向Camera类提供有关我所在位置的信息。

2 个答案:

答案 0 :(得分:0)

例如:

在MainActivity中:

Intent intent = new Intent();
intent.setClass(this, Other_Activity.class);
intent.putExtra("EXTRA_ID", "SOME DATAS");
startActivity(intent);

在Other_Activity中:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String datas= extras.getString("EXTRA_ID");
   if (datas!= null) {
        // do stuff
   }        
}

此处有更多信息: http://developer.android.com/reference/android/content/Intent.html

取自此处:Simple example for Intent and Bundle

答案 1 :(得分:0)

请检查此link ..

它说..

用它来“放”文件......

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String keyIdentifer  = null;
i.putExtra("STRING_I_NEED", strName);
startActivity(i);

然后,要检索值,请尝试以下内容:

String newString;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}