在android中的活动之间传递string
我已经search了不less地方,但我还没有得到任何有效的解决scheme,我真的需要帮助! 我正在做一个应用程序,需要从一个活动传递经纬度string到另一个。 我怎样才能做到这一点? 看看我的代码在这里:LocationActivity.java需要传递string到另一个活动,另一个我没有粘贴到这里。 而需要传递的string被命名为:“latLongString”
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) { } }; }
其他活动:
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类中:
Intent i = new Intent(this, FindAndroidActivity.class); i.putExtra("KEY",YourData);
在FindAndroidActivity类中
Bundle extras = getIntent().getExtras(); if(extras !=null) { String value = extras.getString("KEY"); }
几个情景:
-
如果要在开始新活动时传递string,请将其添加到开始的Intent中,并在新活动的
onCreate
中将其检索。
用Intent.putExtra发送数组// Sending activity String latLong = "test"; Intent i = new Intent(sendingClass.this, receivingClass.class); i.putExtra("latLong", latLong); startActivity(i); // Receiving activity Bundle extras = getIntent().getExtras(); String latLong = extras.getString("latLong");
-
如果要在从活动返回时传递string,请使用
startActivityForResult
并实现onActivityResult
事件
http://micropilot.tistory.com/1577 -
在同一时间运行的两个活动之间传递string的第三个场景是不可能的,因为一次只能运行一个活动(处于前台)。
有时候,意图变得太麻烦和烦人,而是使用一种更简单的(也许不是最优的)devise模式:Singleton。 单身人士就像一个普通的储物盒,可以通过代码访问到,在应用程序的生命周期处于活动状态时存储值。 你也可以把方法放在那里。 单例是一个只能实例化一次的类,可以作为你需要从任何地方访问的所有variables的一站式仓库。 你可以设置/得到任何活动或类,甚至上下文单身的variables! 正如我所说,也许有更好的select,但我没有时间来惩罚自己的意图,空指针,什么不是。 使用下面的代码创build一个新的类,将其称为mySingleton或其他任何东西,并开始设置/获取variables!
public class MySingleton extends Application{ private volatile static appSingleton mInstance = null; private String mystring; private appSingleton(){ mystring="hello"; //initialize your var here //Add all the variables you need, here. public static MySingleton getInstance(){ //Singleton's core if(mInstance == null){ mInstance = new MySingleton(); } return mInstance; } //Place Set and Get methods here public String getMystring(){return this.mystring;} public void setMystring(String s){mystring = s;} //Add get/setmethods for your other variables here } //Thats it
现在,让我们说你想在活动B上设置mystring为“再见”,然后想做到这一点:
MySingleton.getInstance().setMystring("hello");
如果你想从任何其他Activity,类等访问“mystring”,并将其显示在文本框中,只需执行以下操作:
MyTextBox.setText(MySingleton.getInstance().getMystring());
正如你所看到的,你可以在任何地方写入值,并从任何地方用一行代码读取这些值。 请享用!
你的应用程序应该是multithreading? 如果这样,打开一整个蠕虫,parsing数据来回成为一个相当有趣的杂耍。
你看过putextra和getextra函数吗? 他们在活动之间来回parsing数据。 尽pipe我不认为这些工具可以很好地适用于multithreading应用程序。