刷新活动中的方法

时间:2015-12-29 14:19:21

标签: java android

在我的Android应用程序中,我正在使用谷歌地图,我有一个随机数生成器,将从5个位置随机选择一个方法,将找到用户位置。我还添加了一个刷新按钮,它将关闭活动并重新启动它,但我只希望它重新启动查找用户位置方法。当您刷新整个活动时,标志会随机更改我不想要的内容。

Button button2=(Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick (View w){
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            }
        });

这是按下按钮时重启整个活动的方法,但我只想让它刷新此方法的内容

 private void handleNewLocation(Location location) {
        Log.d(TAG, location.toString());
    currentLatitude = location.getLatitude();
    currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    //mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
    MarkerOptions options = new MarkerOptions()
            .position(latLng)
            .title("You are here");
    mMap.addMarker(options);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom((latLng), 11.0F));
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您可以将管理位置更新的代码放入单独的方法中:

public void UpdateLocation()

用以下内容说:

if (isNetworkEnabled) {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    if (locationManager != null) {
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
    if (location == null) {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (locationManager != null) {
            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
              .../...

然后从onclick监听器调用此方法。然后,您可以在此方法中调用handleNewLocation

答案 1 :(得分:0)

如果您重新设置了活动,则可以通过Intent将Location值传递给新活动,例如

Intent intent = getIntent(); // or create a new Intent
finish();
intent.putExtra("LOCATION", myCurrentLocation);
startActivity(intent);

然后,在您选择随机位置的地方,您可以检查意图是否有位置,然后使用它,即:。

Location location;

Intent intent = getIntent();
if(intent.hasExtra("LOCATION")) {
    location = intent.getSerializableExtra("LOCATION");
} else {
    location = getRandomLocation(); // your code for pick a location
}

handleNewLocation(location);

要通过意图将数据传递给新活动,您有以下几种选择:

  • 您可以使Location类可序列化(只需将其implement Serializable)。这是最简单的方法,也就是上面的例子。
  • 为了提高性能,您可以创建课程Parcelable,然后使用getParcelableExtra方法。 (需要更多工作才能使课程Parcelablehttp://developer.android.com/reference/android/os/Parcelable.html
  • 如果您的位置类由基元组成,您可以将这些基元传递给意图,并只是实例化一个新的Location实例,例如,您可以这样做:

    intent.putExtra("LOCATION_LAT", myCurrentLocation.getLatitude());
    intent.putExtra("LOCATION_LONG", myCurrentLocation.getLongitude());