Intent

Intent是Android系统提供的一个信使,作为信息传递的载体。组件与组件之间通过Intent来通信、传递信息、交换数据。

Intent的相关构造函数
1. Intent():Create an empty intent;初始化一个Intent对象。
2. Intent(Intent o):Copy constructor;
3. Intent (String action) :Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system – for example the system VIEW action is android.intent.action.VIEW; an application’s custom action would be something like com.google.app.myapp.CUSTOM_ACTION.
参数action :The Intent action, such as ACTION_VIEW.
隐式Intent的一种用法

3: Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);

4 Intent(packageContext, class):在当前活动启动另一个活动;
5. Intent (String action, Uri uri) :Create an intent with a given action and for a given data url. Note that the action must be in a namespace because Intents are used globally in the system – for example the system VIEW action is android.intent.action.VIEW; an application’s custom action would be something like com.google.app.myapp.CUSTOM_ACTION.;第三个的合并。
6. Intent(action, uri, packageContext, cls):5和4的结合。

Intent的两种启动方式:
1. 显示Intent:Intent的“意图”非常明显。上面的第四种;
2. 隐式Intent:并不明确指出想要启动哪个活动。比如上面的第三种

启动时也是隐式的

<activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

自定义隐式启动另一个活动:

 <activity
            android:name=".SecondActivity"
            <intent-filter>
                <action android:name="com.wangweiwei.java.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.wangweiwei.java.MY_CATEGORY" />
            </intent-filter>
        </activity>

在FirstActivity中

// 每个Intent只能指定一个action,但却能指定多个category
        Intent intent = new Intent("com.wangweiwei.java.ACTION_ATART");
        intent.addCategory("com.wangweiwei.java.MY_CATEGORY");
        startActivity(intent);

使用Intent传递自定义对象:
putExtra()方法中所支持的数据类型是有限的,所以传递自定义对象需用到下面两种方法。
1、实现Serializable:将一个对象转换成可存储或可传输的状态。

/**
 * 来自《第一行代码》
 * @author Administrator
 *
 */
public class Person implements Serializable {

    /**
     * Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,
     * JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,
     * 可以进行反序列化,否则就会出现序列化版本不一致的异常
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

在FirstActivity中:

Person person = new Person();
        person.setName("HaHa");
        person.setAge(22);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("person_data", person);
        startActivity(intent);

在SecondActivity中:

Person person = (Person) getIntent().getSerializableExtra("person_data");

2、 实现Parcelable

/**
 * 来自《第一行代码》
 * @author Administrator
 *
 */
public class Person implements Parcelable {

    /**
     * Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,
     * JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的serialVersionUID进行比较,如果相同就认为是一致的,
     * 可以进行反序列化,否则就会出现序列化版本不一致的异常
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 必须重写describeContents()、writeToParcel()这两个方法
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);// 写出name
        dest.writeInt(age);// 写出age
    }

    // 必须提供的一个常量
    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }

        @Override
        public Person createFromParcel(Parcel source) {
            Person person = new Person();
            person.name = source.readString(); // 读取name
            person.age = source.readInt(); // 读取age
            return person;
        }
    };
}

在FirstActivity中:

Person person = new Person();
        person.setName("HaHa");
        person.setAge(22);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        intent.putExtra("person_data", person);
        startActivity(intent);

在SecondActivity中:

Person person = (Person) getIntent().getParcelableExtra("person_data");

对比:Serializable的方式较为简单,但会把整个对象进行序列化,所以效率比Parcelable低一些,通常情况下使用Parcelable的方式来实现Intent传递对象的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值