**使用意圖鏈接活動**
1、新建一個名為“UsingIntent”的項目,右擊src文件夾下的包名,選擇New-->Class選項,并將新的類文件名命名為“SecondActivity”;
2、打開AndroidManifest.xml文件,添加如下代碼:
~~~
<activity
android:name=".SecondActivity"
android:label="Second Activity" >
<!-- 新活動的意圖篩選器的名稱是net.zenail.SecondActivity,其它活動將通過這個名稱來調用這個活動 -->
<!-- 意圖篩選器的類別是android.intent.category.DEFAULT,其它活動可以通過使用startActivity()方法啟動此活動 -->
<intent-filter>
<action android:name="net.zenail.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
~~~
3、在res/layout文件夾下新建一個secondactivity.xml文件,修改代碼如下:
~~~
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is the Second Activity!" />
</LinearLayout>
~~~
4、打開SecondActivity.java文件,添加如下代碼,添加創建方法:
~~~
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
}
~~~
5、在activity_main.xml文件中添加如下代碼,新建一個Button:
~~~
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Display second activity" />
~~~
6、在MainActivity.java中添加如下代碼,添加點擊方法:
~~~
public void onClick(View v) {
startActivity(new Intent("net.zenail.SecondActivity"));// 將意圖篩選器的名稱傳進去
// 如果要調用的活動是定義在同一個項目中,則可以重寫上面的方法: startActivity(new Intent(this,
// SecondActivity.class));
}
~~~
7、運行,效果如下:


**[點擊下載完整代碼~](http://download.csdn.net/detail/u012904198/7309201)**
- 前言
- Android應用程序剖析
- (一)——生命周期
- (二)——使用Intent傳數據之通用方式
- (三)——使用靜態變量傳遞數據
- (四)——通過剪切板傳遞數據
- (五)——通過全局變量傳遞數據
- (六)——從Activity返回數據
- adt-bundle-linux-x86_64-20131030下新建工程提示找不到adb和R.java問題的解決
- Eclipse啟動時提示fail to create the Java Virtual Machine問題的解決
- Android常見UI組件之ListView(一)
- Android常見UI組件之ListView(二)——定制ListView
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)