这里总结了Android中几种对象共享的方式,便于以后查阅。

一,基于消息的通信机制  Intent : Boudle   Extra 这种方式比较常见,有很多的介绍,主要通过 Intent类型,将要传递的数据与一个key绑定,在另一Activity中通过key进行取值。但此方式的问题是可以传递 (共享)的 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream, 或者LinkList链表等等数据类型就不太好用。有时候不得已需要自己去实现序列化的接口。它比较适用于传递一些简单类型的值。

在SendActivity中定义的函数:

private void ExecIntentTransfer()
{
String strValue = m_teValue.getText().toString();
if ((strValue == "") || (strValue.length() == 0))
return;
Intent intent = new Intent();
intent.setClassName( getApplicationContext(), "com.example.activitysharedata.ActivitySharewithIntent" );
Bundle b = new Bundle();
b.putString("key", strValue);
intent.putExtras( b );
startActivity(intent);
System.exit(0);
}

在接收函数中定义的函数:

public class ActivitySharewithIntent extends Activity
{
private TextView m_tvValue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
m_tvValue = (TextView)findViewById(R.id.tvValue);
String strValue = getIntent().getExtras().getString("key");
m_tvValue.setText(strValue);
}
}

其中,接收端用到的key和发送端的要保持一致。

二,利用static静态数据, public static成员变量

这种方式使用简单,缺点是使用了静态全局变量,对于多线程或有多个模块修改其值的时候,会有一定的风险。它可以传递任意类型的变量。

发送端的程序,其实就是向全局变量赋值。 注意这里要用 this.finish()函数,而不能使用System.exit()函数,否则对象被销毁值也就不存在了,无法完成传递。

private void ExecStaticTransfer()
{
String strValue = m_teValue.getText().toString();
if ((strValue == "") || (strValue.length() == 0))
return;
Intent intent = new Intent();
intent.setClassName( getApplicationContext(), "com.example.activitysharedata.ActivitySharewithStatic" );
ActivitySharewithStatic.Value = strValue;
startActivity(intent);
this.finish();
}

接收端的程序,很简单,只是从静态变量中取值。

public class ActivitySharewithStatic  extends Activity
{
private TextView m_tvValue;
public static String Value;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
m_tvValue =  (TextView)findViewById(R.id.tvValue);
m_tvValue.setText(Value);
}
}

三,基于外部存储的传输,  File/SharedPreference/ Sqlite ,如果要针对第三方应用需要Content Provider

这里File和Sqlite比较好理解,文件和数据库也比较常用,都是可以将数据固化在系统中。 此处将使用SharedPerference的例子。

这里主要使用的是Editor,向Editor中添加键,并且存储相应的值,在使用时再通过键把值取出。

发送端:

private void ExecSharedPerferenceTransfer()
{
String strValue = m_teValue.getText().toString();
if ((strValue == "") || (strValue.length() == 0))
return;
SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("value", strValue);
editor.commit();
Intent intent = new Intent();
intent.setClassName( getApplicationContext(), "com.example.activitysharedata.ActivitySharewithExtStorage" );
startActivity(intent);
this.finish();
}

接收端:

public class ActivitySharewithExtStorage extends Activity
{
private TextView m_tvValue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
SharedPreferences sharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE);
String strValue =sharedPreferences.getString("value", "");
m_tvValue = (TextView)findViewById(R.id.tvValue);
m_tvValue.setText(strValue);
}
}

四,基于Application Context

这里使用的方式主要是通过 Application这个对象,每个App都可以看做一个Applicatin, 它的当前App的名字可以在AndroidManifest.xml中找到。

比如说Demo中的App的名字被定义为 “ActivityShareDataApp”,保存在 AndroidManifest.xml中。

package="com.example.activitysharedata"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="17"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="ActivityShareDataApp">
android:name="com.example.activitysharedata.MainActivity"
android:label="@string/app_name" >
定义了名字,要在工程中添加一个同名的Application类。
ActivityShareDataApp.java
public class ActivityShareDataApp extends Application
{
public String Value;
@Override
public void onCreate()
{
super.onCreate();
}
}

这个类中定义的成员有全局变量的性质,可以在整个App中使用。

发送端:在发送端其实就是简单的给Application类中的成员赋值就可以了

private void ExecAppContext()
{
ActivityShareDataApp mApp;
String strValue = m_teValue.getText().toString();
if ((strValue == "") || (strValue.length() == 0))
return;
mApp = (ActivityShareDataApp)getApplication();
mApp.Value = strValue;
Intent intent = new Intent();
intent.setClassName( getApplicationContext(), "com.example.activitysharedata.ActivitySharewithAppContext" );
startActivity(intent);
this.finish();
}

接收端:简单的在Application中取值即可。

public class ActivitySharewithAppContext extends Activity
{
private TextView m_tvValue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
ActivityShareDataApp mApp;
mApp = (ActivityShareDataApp)getApplication();
String strValue = mApp.Value;
m_tvValue = (TextView)findViewById(R.id.tvValue);
m_tvValue.setText(strValue);
}
}