1.Activity序列化数据传递
package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
Button btn_serializabale;
EditText et_phone,et_name,et_major,et_age;
int age;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_serializabale=(Button)findViewById(R.id.zerializable);
btn_serializabale.setOnClickListener(this);
et_phone=(EditText)findViewById(R.id.et_phone);
et_name=(EditText)findViewById(R.id.et_name);
et_major=(EditText)findViewById(R.id.et_major);
et_age=(EditText)findViewById(R.id.et_age);
}
@Override
public void onClick(View v) {
//获取电话,姓名,专业,年龄
String phone=et_phone.getText().toString();
String name=et_name.getText().toString();
String major=et_major.getText().toString();
String ageString=et_age.getText().toString();
try {
age=Integer.parseInt(ageString);
}catch(Exception e){
e.printStackTrace();
}
Student stu=new Student();
stu.setPhone(phone);
stu.setName(name);
stu.setMajor(major);
stu.setAge(age);
Bundle bundle=new Bundle();
bundle.putSerializable("student",stu);
Intent intent=new Intent(MainActivity.this,BActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
}
package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class BActivity extends Activity {
TextView t_phone,t_name,t_major,t_age;
Button btn_tel;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
Student stu=(Student)bundle.getSerializable("student");
if(stu!=null){
String tv1=stu.getPhone();
String tv2=stu.getName();
String tv3=stu.getMajor();
int tv4=stu.getAge();
String tv5=String.valueOf(tv4);
t_phone=(TextView)findViewById(R.id.tv_phone);
t_name=(TextView)findViewById(R.id.tv_name);
t_major=(TextView) findViewById(R.id.tv_major);
t_age=(TextView)findViewById(R.id.tv_age);
t_phone.setText(tv1);
t_name.setText(tv2);
t_major.setText(tv3);
t_age.setText(tv5);
}
btn_tel=(Button)findViewById(R.id.btn_tel);
btn_tel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone=t_phone.getText().toString();
Intent it=new Intent();
Uri callUri=Uri.parse("tel://"+phone);
it.setAction(Intent.ACTION_DIAL);
it.setData(callUri);
startActivity(it);
}
});
}
}
package zhku.edu.exp5_1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class TelActivity extends Activity {
TextView tv_tel;
Student st=new Student();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tel);
tv_tel=(TextView)findViewById(R.id.tv_tel);
Intent intent=getIntent();
//法1;Uri uri=Uri.parse("tel:12345678")可通过uri.toString()输出:tel:12345678
//法2:Uri uri=Uri.parse("tel://12345678")可通过uri.getHost()输出:12345678
String out=intent.getData().getHost();
tv_tel.setText(out);
}
}
package zhku.edu.exp5_1;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID=1;
private String phone;
private String name;
private String major;
private int age;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Activity_main.xml
Activity_b.xml
Activity_tel.xml
AndriodMainfest.xml
2. Activity的SharedPreference
MainActivity.java
package zhku.edu.exp5_2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Date;
import java.util.Map;
public class MainActivity extends Activity{
TextView tv_1,tv_2;
Button btn_clear;
Map UserInfo;
int count;
String date;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLoginDate();
initView();
changeLoginDate();
}
public void initView(){
tv_1=(TextView)findViewById(R.id.tv_count);
tv_2=(TextView)findViewById(R.id.tv_date);
btn_clear=(Button)findViewById(R.id.btn_clear);
tv_1.setText((count+1)+"");
tv_2.setText(date);
btn_clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MyUtils.clearUserInfo(MainActivity.this);
tv_1.setText("0");
tv_2.setText("无");
}
});
}
public void getLoginDate(){//获取登录信息
UserInfo=MyUtils.getUserInfo(this);
count=(Integer)UserInfo.get(MyUtils.NUM);
date=(String)UserInfo.get(MyUtils.TIME);
}
public void changeLoginDate(){//修改登录信息
MyUtils.saveUserInfo(this,count+1,new Date());
}
}
MyUtils.java
package zhku.edu.exp5_2;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class MyUtils {// 创建工具类封装
public static final String NAME="data";
public static final String NUM="count";
public static final String TIME="date";
public static void saveUserInfo(Context context, int count, Date date){
//保存Preference
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putInt(NUM, count);//更新登录次数
DateFormat df = new SimpleDateFormat("yyyy‐MM‐dd hh:mm:ss");//更新登录时间
edit.putString(TIME, df.format(date));//格式化当前日期,以字符串保存
edit.commit();
}
public static Map getUserInfo(Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
int count = sp.getInt(NUM, 0);//获取登录次数
String date = sp.getString(TIME, "无");//获取上次登录时间
Map data = new HashMap();
data.put(NUM, new Integer(count));
data.put(TIME, date);
return data;//以Map 数据返回
}
public static void clearUserInfo(Context context) {
SharedPreferences sp = context.getSharedPreferences(NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.clear();
edit.commit();
}
}