Intent的基本作用:
一個Intent對象包含了一組資訊:
1. Component name
2. Action
3. Date
4. Category
5. Extras
6. Flags
Intent 概述
• Intent是Android的核心組件,利用訊息實現應用程式間的互動機制,這種訊息描述了應用中一次操作的動作、資料以及附加資料,系統通過該Intent的描述負責找到對應的組件,並將Intent傳遞給調用的組件,完成組件的調用。
• Intent由動作、資料、分類、類型、組件和擴充資訊等內容組成,每個組成都由相應的屬性進行表示,並提供設定和擷取相應屬性的方法
| 成 |
屬性 |
設定屬性方法 |
擷取屬性方法 |
| 動作 |
Action |
setAction() |
getAction() |
| 資料 |
Data |
setData() |
getData() |
| 分類 |
Category |
setCategory() |
|
| 類型 |
Type |
setType() |
getType() |
| 組件 |
Component |
setComponent() setClass() setClassName() |
getComponent() |
| 擴充資訊 |
Extra |
putExtra() |
getXXXExtra()擷取不同資料類型的資料,如int類型則使用getIntExtra(),字串則使用getStringExtra() getExtras()擷取Bundle包 |
在Android中,Intent和Activity是直接相互操作的。Intent的最常見的用途是繫結應用程式組件。Intent用來在應用程式的Activity間啟動、停止和傳輸。
為了開啟應用程式中的不同的介面(對應一個Activity),調用startActivity,傳入一個Intent,如:startActivity(myIntent);
由我們可以大致瞭解Activity和Intent之間的關係。
下面通過一個執行個體來說明如何利用Intent對Activity進行操作的:
1.建立一個Android項目(Activity02)
代碼清單分別為:
1)Android02.java
2)otherActivity.java
3) main.xml
4)other.xml
5) string.xml
6)Android02Manifest.xml
=》》Android02.java
[java] <span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;
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 Activity02 extends Activity {
/** Called when the activity is first created. */
private Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText("myButton");
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//產生一Intent對象
Intent intent = new Intent();
intent.setClass(Activity02.this,otherActivity.class);
Activity02.this.startActivity(intent);
}
}
}</span>
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;
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 Activity02 extends Activity {
/** Called when the activity is first created. */
private Button myButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.myButton);
myButton.setText("myButton");
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//產生一Intent對象
Intent intent = new Intent();
intent.setClass(Activity02.this,otherActivity.class);
Activity02.this.startActivity(intent);
}
}
}</span>
=》》otherActivity.java
[java]
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class otherActivity extends Activity{
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}
}
</span>
<span style="font-family:FangSong_GB2312;font-size:18px;">package com.Activity02;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class otherActivity extends Activity{
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);
}
}
</span>
=》》main.xml
[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout></span>
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<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/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout></span>
=》》other.xml
[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</span>
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</span>
=》》string.xml
[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Activity02!</string>
<string name="app_name">Activity02</string>
<string name="other">otherActivity</string>
</resources></span>
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Activity02!</string>
<string name="app_name">Activity02</string>
<string name="other">otherActivity</string>
</resources></span>
=》》Android02Manifest.xml
[html]
<span style="font-family:FangSong_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Activity02"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Activity02"
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=".otherActivity" android:label="@string/other"/>
</application>
</manifest></span>
模擬器運行結果:
按下Button按鈕就執行otherActivity
摘自 wwj_748