在Android的活动之间传递字符串 [英] Passing strings between activities in android

查看:86
本文介绍了在Android的活动之间传递字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅了不少地方,但我没有起身与工作还没有什么好的解决办法,我真的需要帮助! 我提出,需要从一个活动传递一个经度和纬度串到另一个应用程序。 我怎样才能做到这一点???看看我的code所在位置:LocationActivity.java需要将字符串传递给其他的活动,到另外一个,我没有贴到这里。这需要传递被命名为字符串:latLongString

I have searched quite a few places but i haven't got up with any good solution that worked yet, and i really need help! I am making an application that needs to pass a longitude and latitude string from one activity to another. How can i do this??? Take a look at my code here: The LocationActivity.java needs to pass the string to the other activity and to another one that i didn't paste into here. And the string that needs to be passed is named: "latLongString"

LocationActivity.java:

LocationActivity.java:

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class LocationActivity extends Activity {
private String Location;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LocationManager locManager;
    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, locationListener);
    Location location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    updateWithNewLocation(location);
    if(location != null)                                
    {
        double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    }  
  }


private void updateWithNewLocation(Location location) {
    TextView myLocationText = (TextView)findViewById(R.id.LocationWord);
    String latLongString = "";
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLongString = "Lat:" + lat + "\nLong:" + lng;
        //This is where i need to pass the string to the other activity

    } else {
        latLongString = "No location found";
    }
     myLocationText.setText("Your Current Position is:\n" +
            latLongString);
}

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
        updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};


}

另一项活动:

The other activity:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Locale;

import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.gsm.SmsMessage;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.location.LocationManager;
import android.location.LocationListener;

public class FindAndroidActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button Nextbutton1, Nextbutton2, Nextbutton3, TestLocationService, EditSettings;
TextView Cordinates, Adress, FindAndroid, TextView;
EditText LocationWord;
private LocationManager locManager;
private LocationListener locListener;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       setContentView(R.layout.firsttimescreen);
       Nextbutton1 = (Button)findViewById(R.id.Nextbutton1);
       Nextbutton1.setOnClickListener(this);
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.Nextbutton1:
        setContentView(R.layout.setup);
        Nextbutton2 = (Button)findViewById(R.id.Nextbutton2);
        TestLocationService = (Button)findViewById(R.id.TestLocationService);
        TestLocationService.setOnClickListener(this);
        Nextbutton2.setOnClickListener(this);
        break;
    case R.id.Nextbutton2:
        setContentView(R.layout.setup1);
        Nextbutton3 = (Button)findViewById(R.id.Nextbutton3);
        LocationWord = (EditText)findViewById(R.id.LocationWord);
        LocationWord.requestFocus(View.FOCUS_DOWN);
        Nextbutton3.setOnClickListener(this);
        break;
    case R.id.Nextbutton3:
        setContentView(R.layout.main);
        EditSettings = (Button)findViewById(R.id.EditSettings);
        EditSettings.setOnClickListener(this);
        break;
    case R.id.TestLocationService:
        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
            String value = extras.getString("KEY");             
        }
        Cordinates = (TextView)findViewById(R.id.Cordinates);
        Cordinates.setText(value);
            //This does not work because the string "value" isn't availible outside the braces,
            //obviously. How do i make it availible there???
        break;
    case R.id.EditSettings:
        setContentView(R.layout.setup1);
        Nextbutton3 = (Button)findViewById(R.id.Nextbutton3);
        LocationWord = (EditText)findViewById(R.id.LocationWord);
        LocationWord.requestFocus(View.FOCUS_DOWN);
        Nextbutton3.setOnClickListener(this);
        break;
    }
}

}

推荐答案

在你的LocationActivity类:

In your LocationActivity class:

Intent i = new Intent(this, FindAndroidActivity.class);
i.putExtra("KEY",YourData);

在FindAndroidActivity类

In FindAndroidActivity class

Bundle extras = getIntent().getExtras();
if(extras !=null) {
    String value = extras.getString("KEY");
}

这篇关于在Android的活动之间传递字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆