本篇文章将和大家一起探讨以下两个问题:
1. 什么是意图
2. 编写通过意图传递数据的小demo
首先,我们查看一下Andriod关于意图(Intent)给我们提供的API的相关说明。
a、类简介
意图是对要执行的操作进行抽象的描述。它可以通过startActivity启动一个Activity,broadcastintent发送给任何有兴趣的BroadcastReceiver组件,和startService(意图)或bindService(意图,serviceconnection,int)与后台服务沟通。
意图为在不同的应用程序中执行的代码之间进行后期绑定提供了一个设施。其最重要的用途是存在于activities当中,在那里它可以被认为是活动之间的胶水。它基本上是一个被动的数据结构,它需要一个抽象的描述来执行一个动作。
b、意图的结构
意图主要表达的信息是:
行动:要进行一般的动作,如action_view,action_edit,action_main,等。
数据:数据操作,如联系人数据库一个人记录,表示为一个URI。
c、意图的构造方法
// 创建一个空的意图
Intent()
Create an empty intent.
// 拷贝构造器
Intent(Intent o)
Copy constructor.
// 用一个给定的动作创建一个意图
Intent(String action)
Create an intent with a given action.
// 创建一个给定的行动意图和一个给定的数据。
Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.
// 意图(上下文,类名)
Intent(Context packageContext, Class
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="通过意图传递数据">
</Button>
</LinearLayout>
2、新建一个other.xml,添加一个TextView用于接收从上一个activity传递过来的数据
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
3、在MainActivity当中,我们创建意图,并通过putExtra()方法,设置传递的数据(键值对)。
package com.example.intentpassvalue;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 声明一个意图
Intent intent = new Intent(MainActivity.this,Other.class);
// 通过意图的putExtra方法传递键值对
intent.putExtra("name", "tmac");
intent.putExtra("age", 24);
intent.putExtra("address", "北京");
// 启动意图
startActivity(intent);
}
});
}
}
4、创建Other类,并通过getIntent()获取一个意图对象,然后调用意图的getIntExtra()\getStringExtra()等方法获取从上一个activity当中传递过来的数据。
package com.example.intentpassvalue;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Other extends Activity {
private TextView textview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textview1 = (TextView)this.findViewById(R.id.textview1);
// 声明意图,接收数据
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
String address = intent.getStringExtra("address");
textview1.setText("name = " + name + " age = " + age + " address = " + address);
}
}
总结:
1、意图是运行时的绑定机制,它是一个抽象的概念,主要用于不同activity之间的通讯,当然不限于activity,有可能是其他的组件。
2、在这些组件的通讯过程中,意图负责应用中一次操作的动作、动作涉及的数据以及附加数据进行描述。Android则根据此意图的描述负责找到对应的组件,将Intent传递给对应的组件,并完成组件的调用。