Intent的概念及應用,以及Intentfilter過濾器的選項配置

Intent包括顯示Intent和隱式Intent
手動創建一個Activity:
創建一個class繼承於Activity,裏面要重構一個onCreat()函數,

protected void onCreat(Bundle savedInstanceState){
              super.onCreat(savedInstanceState);
              setContentView(R.layout.xxx);//通過setContentView()綁定視圖文件XML

}
之後在AndroidManifest.xml中的application中,添加以下語句:

<activity android:name=".xxx"
/>

startActivity(new Intent(MainActivity.this, xxx.class));//啓動該Activity,這就是顯式的Intent,指定了要執行的Activity爲xxx

在隱式Intent中,
在AndroidManifest.xml中的application中,添加以下語句:

<activity
    android:name=".AnotherAty"
android:label="@string/title_activity_another_aty" >
<intent-filter>//過濾器
    <action android:name="com.xxx.project.intent.action.aty"/>//定義該Acitivity 的 name
    <category android:name="android.intent.category.DEFAULT"/>//指定它是一個Acitivity
</intent-filter>
</activity>
startActivity(new Intent(“com.xxx.project.intent.action.aty”));//啓動該Activity,這就是隱式的Intent,指定了要執行的Activity爲AnotherAty

com.xxx.project.intent.action.aty這個字符串還可以在Activity類的定義中,添加爲常量字符串。
通過隱式的Intent可以在一個程序中,調用另一個APP的activity。
如果需要規定該ACTIVITY只能在同一個APP中被調用,則可以在manifest中的activity定義裏添加:
android: exported = “false”;

intentfilter過濾器可以用於設置匹配信息,包括action,category,還有data.
category可以定義是否是Acitivity(

            <category
android:name="android.intent.category.DEFAULT"/>),

是否允許被瀏覽器訪問調用(

<category android:name="android.intent.category.BROWSABLE"/>)

data可以定義協議Scheme/參數等。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章