两个活动之间的通信?

10 浏览
0 Comments

两个活动之间的通信?

我正在制作一个使用GoogleMaps的应用程序。现在,我有一个关于MapActivity和ToursActivity之间通信的问题。为了简化,我的应用程序是关于乐队Metallica的,并且我有一些巡演的列表。当用户点击其中一个巡演时,它应该打开一个带有该位置的新活动。我不会在这里放ToursActivity,因为有一堆你不需要的代码。另外,我将所有的数据都保存在Firebase上,如果这很重要的话。\n这是我的MapActivity:\n

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final int REQUEST_LOCATION_PERMISSION = 10;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
@BindView(R.id.lvTours) ListView lvTours;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tours_coord);
    this.initialize();
    lvTours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
        }
    });
}
public void initialize(){
    this.mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fGoogleMap);
    this.mMapFragment.getMapAsync(this);
    this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions newMarkerOptions = new MarkerOptions();
            newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
            newMarkerOptions.title("Tour");
            newMarkerOptions.snippet("It' was here!");
            newMarkerOptions.position(latLng);
            mGoogleMap.addMarker(newMarkerOptions);
        }
    };
}
@Override
public void onMapReady(GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    UiSettings uiSettings = this.mGoogleMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);
    uiSettings.setMyLocationButtonEnabled(true);
    uiSettings.setZoomGesturesEnabled(true);
    this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
   goToLocation(33.835293 , -117.914505);
}
public void goToLocation(double lat, double lng){
    LatLng latLng = new LatLng(lat, lng);
    CameraPosition position = CameraPosition.builder()
            .target(latLng)
            .zoom(16f)
            .bearing(0.0f)
            .tilt(0.0f)
            .build();
    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position),null);
}
private boolean hasLocationPermission() {
    String LocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
    int status = ContextCompat.checkSelfPermission(this, LocationPermission);
    if (status == PackageManager.PERMISSION_GRANTED) {
        this.mGoogleMap.setMyLocationEnabled(true);
        return true;
    }
    return false;
}
private void requestPermission() {
    String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    ActivityCompat.requestPermissions(MapActivity.this, permission, REQUEST_LOCATION_PERMISSION);
}
}

0
0 Comments

问题的出现原因是在ToursFragment中使用Intent打开MapActivity时,参数(latitude和longitude)的构造方法无法解析。解决方法是使用正确的构造方法和参数来创建Intent对象。

解决方法如下所示:

(R.id.lvTours) public void onItemClick(View v, int position) { 
    Intent intent = new Intent(v.getContext(), MapActivity.class); 
    intent.putExtra("latitude", lat); 
    intent.putExtra("longitude", lng ); 
    startActivity(intent); 
}

在MapActivity中,通过获取Intent和Bundle来获取传递的参数值:

Intent intent = getIntent();
Bundle extras = intent.getExtras();
double latitude = intent.getDouble("latitude");
double longitude = intent.getDouble("longitude");

此外,还可以使用正确的构造方法来创建Intent对象:

Intent intent = new Intent(getContext(), MapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);

这样就可以解决参数构造方法无法解析的问题。

0
0 Comments

问题的原因:在两个活动之间进行通信时,需要将数据传递给另一个活动。这可能是因为需要在不同的活动之间共享数据或传递某些参数。

解决方法:可以使用Intent对象来传递额外的参数。在发送方活动中,可以使用putExtra()方法将数据添加到Intent对象中,并通过startActivity()方法启动接收方活动。在接收方活动中,可以使用getIntent()方法获取传递的Intent对象,并使用getXXXExtra()方法获取传递的数据。

例如,在ToursActivity.java中,可以使用以下代码将参数传递给MapActivity活动:

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
startActivity(intent);

然后,在MapActivity.java的onCreate()方法中,可以使用以下代码获取这些参数:

Intent intent = getIntent();
long latitude = intent.getLongExtra("lat", 0);
long longitude = intent.getLongExtra("long", 0);

通过这种方式,就可以在两个活动之间传递数据并进行通信。

感谢快速回答。

0