标签:des android style blog http color io os 使用
四种传递方法:
1.通过Intent传递数据;
2.通过静态变量传递数据;
3.通过剪切板传递数据;
4.通过全局对象传递数据;
分类介绍:
1.通过Intent传递数据;
代码如下:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/btnintent" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过Intent传递数据" android:textSize="18sp" /> </LinearLayout>可序列化类的代码:
package com.example.transdata; import java.io.Serializable; public class Data implements Serializable{ public int id; public String name; }第一个Activity的代码:
package com.example.transdata; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btnintent); btn.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = null; switch (v.getId()) { case R.id.btnintent: intent = new Intent(this, OtherActivity.class); intent.putExtra("intent_string", "通过Intent传递的字符串"); intent.putExtra("intent_integer", 300); Data data = new Data(); data.id = 1000; data.name = "Android"; intent.putExtra("intent_object", data); startActivity(intent); break; default: break; } } }
package com.example.transdata; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); TextView tvWord = (TextView) findViewById(R.id.tv_word); String intentString = getIntent().getStringExtra("intent_string"); int intentInteger = getIntent().getExtras().getInt("intent_integer"); Data data = (Data) getIntent().getExtras().get("intent_object"); StringBuffer sb = new StringBuffer(); sb.append(intentString); sb.append("\n"); sb.append(intentInteger); sb.append("\n"); sb.append(data.id); sb.append(data.name); tvWord.setText(sb.toString()); } }
intent.putExtra("intent_string", "通过Intent传递的字符串"); intent.putExtra("intent_integer", 300); Data data = new Data(); data.id = 1000; data.name = "Android"; intent.putExtra("intent_object", data);
String intentString = getIntent().getStringExtra("intent_string"); int intentInteger = getIntent().getExtras().getInt("intent_integer"); Data data = (Data) getIntent().getExtras().get("intent_object");
StringBuffer sb = new StringBuffer(); sb.append(intentString); sb.append("\n"); sb.append(intentInteger); sb.append("\n"); sb.append(data.id); sb.append(data.name); tvWord.setText(sb.toString());
Intent传递数据具有局限性,Intent无法传递不能序列化的对象。这时候使用静态变量传递数据可以解决。
新建的一个Activity的代码如下:
package com.example.transdata; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity2 extends Activity { public static String name; public static int id; public static Data data; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other2); TextView tvWord = (TextView) findViewById(R.id.tv_word); StringBuffer sb = new StringBuffer(); sb.append(name); sb.append("\n"); sb.append(id); sb.append("\n"); sb.append(data.id); sb.append("\n"); sb.append(data.name); tvWord.setText(sb.toString()); } }
case R.id.btnstatic: intent = new Intent(this, OtherActivity2.class); OtherActivity2.id = 3000; OtherActivity2.name="保时捷"; OtherActivity2.data = new Data(); OtherActivity2.data.id = 1000; OtherActivity2.data.name = "Android"; startActivity(intent); break;
剪切板只能存储简单类型的数据类型或可序列化的对象,对于某些不可序列化的对象如果可以将其转换为字节流(字节数组),也可以将这些对象保存到剪切板中。本例中不是直接将Data对象保存在剪切板中,而是将Data对象转换为Base64编码后以字符串的形式存储在剪切板中。
MainActivity主要代码如下:
case R.id.btnClipboard: intent = new Intent(this, OtherActivity3.class); //获取剪切板的对象 ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); Data clipdata = new Data(); clipdata.id = 6666; clipdata.name = "testclipdata"; //创建字节数组输出流对象,用于将Data对象转换为字节流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); //存储的字符串 String base64Str = ""; try { //对象流 ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(clipdata); //将字节流进行Base64编码 base64Str = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); oos.close(); } catch (IOException e) { e.printStackTrace(); } //获取存储文本数据的剪切板数据的对象 ClipData text = ClipData.newPlainText("data", base64Str); //设置主剪切板 clipboard.setPrimaryClip(text); startActivity(intent); break;
显示数据的Activity:
package com.example.transdata; import java.io.ByteArrayInputStream; import java.io.ObjectInputStream; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.ClipboardManager; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.util.Base64; import android.widget.TextView; @SuppressLint("NewApi") public class OtherActivity3 extends Activity { @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other3); TextView tvWord = (TextView) findViewById(R.id.tv_word); ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); //从剪切板中获取Base64编码的字符串 String base64Str = manager.getPrimaryClip().getItemAt(0).getText().toString(); //将Base64编码字符串解码成字节数组 byte[] buffer = Base64.decode(base64Str, Base64.DEFAULT); ByteArrayInputStream bais = new ByteArrayInputStream(buffer); try { ObjectInputStream ois = new ObjectInputStream(bais); //将字节流还原为Data对象 Data data = (Data) ois.readObject(); tvWord.setText(base64Str+":" + data.id + ":" + data.name); } catch (Exception e) { e.printStackTrace(); } } }
使用静态变量具有局限性(1.大量使用静态变量会造成内存溢出;2.静态变量在很多类中出现容易造成代码难以维护和混乱的情况)。
全局对象所对应的类必须从android.app.Application继承。
全局类:
package com.example.transdata; import android.app.Application; public class MyApplication extends Application { //必须有一个无参的构造器 public String country; public Data data = new Data(); }
case R.id.btnApplication: //必须先获取全局对象 MyApplication application = (MyApplication) getApplicationContext(); application.country = "china"; application.data.id = 123; application.data.name = "testapplication"; intent = new Intent(this, OtherActivity4.class); startActivity(intent); break;
package com.example.transdata; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity4 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other4); TextView tvWord = (TextView) findViewById(R.id.tv_word); MyApplication application = (MyApplication) getApplicationContext(); tvWord.setText(application.country + ":" + application.data.id + ":" + application.data.name); } }
传递基本类型或可序列化的数据,用Intent;
传递不可序列化的对象,最好使用全局对象;如果想让某些数据长时间驻留在内存,最好使用全局对象。
标签:des android style blog http color io os 使用
原文地址:http://blog.csdn.net/eeeduo/article/details/39962485