Activity顯示/隱式調用,主要通過intent-filter實現
其中category的各項屬性值及含義
常量 |
含義 |
CATEGORY_BROWSABLE |
目標activity可以使用瀏覽器來顯示-例如圖片或電子郵件消息. |
CATEGORY_GADGET |
該activity可以被包含在另外一個裝載小工具的activity中. |
CATEGORY_HOME |
該activity顯示主屏幕,也就是用戶按下Home鍵看到的界面. |
CATEGORY_LAUNCHER |
該activity可以作爲一個任務的第一個activity,並且列在應用程序啓動器中. |
CATEGORY_PREFERENCE |
該activity是一個選項面板. |
package com.test;
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;
/**
* @author WonderCoder
* @time 2012-3-30
* Activity間的切換
*
*/
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt1 = (Button)findViewById(R.id.button1);
Button bt2 = (Button)findViewById(R.id.button2);
Button bt3 = (Button)findViewById(R.id.button3);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
}
public void onClick(View view) {
Intent intent = null;
switch(view.getId()){
case R.id.button1:
intent = new Intent(this,MyActivity1.class);
break;
case R.id.button2:
intent = new Intent("myaction1");
break;
case R.id.button3:
intent = new Intent("myaction2");
intent.addCategory("mycategory");
break;
}
startActivity(intent);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="顯示調用Activity"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="隱式調用Activity"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="隱式調用兩個符合過濾條件的Activity"
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
運行結果: