天天看點

Intent的七大屬性, ComponentName、  Action 、 Category 、 Data  、Type、  Extra  、Flags。

目錄

​​Intent的七大屬性:​​

​​ComponentName、  Action 、 Category 、 Data  、Type、  Extra  、Flags。​​

​​(一)、ComponentName屬性:​​

​​(二)、Action、Category屬性與intent-filter配置:​​

​​(三)、Category 屬性:​​

​​(四)、Data屬性:​​

​​(五)、Type屬性:​​

​​(六)、Extra屬性:​​

​​ 4、 Intent利用Action屬性中的ACTION_GET_CONTENT擷取傳回值:​​

​​(七)、Flags屬性:Intent可調用addFlags()方法來為Intent添加控制标記。​​

​​Activity的launchMode【知識點回顧】:​​

Intent的七大屬性:

ComponentName、  Action 、 Category 、 Data  、Type、  Extra  、Flags。

Action作為辨別符,代表一個Intent,當一個Activity需要外部協助處理時,就會發出一個Intent,如果一個程式能完成相應功能,隻要在intent-filter加上這個這個intent就可以了。

Data儲存需要傳遞的資料格式,比如:tel://

Extras儲存需要傳遞的額外資料。

Category表示Intent的種類,從android上啟動Activity有多種方式,比如 程式清單、桌面圖示、點選Home激活的桌面等等,Category則用來辨別這些Activity的圖示會出現在哪些啟動的上下文環境裡。

(一)、ComponentName屬性:

1、指定了ComponentName屬性的Intent已經明确了它将要啟動哪個元件,是以這種Intent被稱為顯式Intent,沒有指定ComponentName屬性的Intent被稱為隐式Intent。隐式Intent沒有明确要啟動哪個元件,應用會根據Intent指定的規則去啟動符合條件的元件。

2、示例代碼:

Intent intent = new Intent();
ComponentName cName = new ComponentName(MainActivity.this,NextActivity.class);
intent.setComponent(cName);
startActivity(intent);

//實際上,以上的寫法都被簡化為以下寫法:
Intent intent = new Intent(MainActivity.this,NextActivity.class);
        startActivity(intent);

//也就是說,平時我們最常用的Intent頁面跳轉的寫法就調用的是顯式Intent。


此外,ComponentName屬性可以實作一個app跳轉到另一個app。

Intent intent = new Intent();
   ComponentName cName = new ComponentName(
"com.steven.testasyncloader.sqlitedata","com.steven.testasyncloader.sqlitedata.MainActivity");
//其中兩個參數的含義:第一個是要跳轉到的app的包名,第二個參數是該包中的要跳轉到app的頁面的class
   intent.setComponent(cName);
   startActivity(intent);      

(二)、Action、Category屬性與intent-filter配置:

通常,Action、Category屬性結合使用,定義這兩個屬性都是在配置檔案的<intent-filter>節點中。Intent通過定義Action屬性(其實就是一段自定義的字元串),這樣就可以把Intent與具體的某個Activity分離,實作了解耦。否則,每次跳轉,都要寫成類似new Intent(MainActivity.this,NextActivity.class)這樣的形式,也就是說必須将要跳轉的目标Activity的名字寫出來,這樣的編碼其實是“寫死”,并沒有實作松耦合。調用Intent對象的setAction()方法實作頁面跳轉雖然略微複雜(需要在AndroidManifest.xml檔案中配置),但是實作了解耦。

1、示例代碼:

Intent intent = new Intent();
intent.setAction("com.steven.android06lifecycle.nextactivity");
startActivity(intent);

//在配置檔案中注冊Activity的時候需要聲明:
<activity android:name="com.steven.android06lifecycle.NextActivity">
<intent-filter>
<action android:name="com.steven.android06lifecycle.nextactivity" />
<category android:name="android.intent.category.DEFAULT" />   
</intent-filter>
</activity>

//當某個頁面是預設啟動頁面時,需要定義Action、Category的屬性必須為以下字元串:【設定任務入口】
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>      

2、常用Action屬性常量:

        Intent對象不僅可以啟動本應用内的程式元件,也可以啟動Android系統的其他應用的元件,包括系統内置的程式元件(需要設定權限)。     

ACTION_MAIN:(android.intent.action.MAIN)Android程式入口。
        每個Android應用必須且隻能包含一個此類型的Action聲明。【如果設定多個,則哪個在前,執行哪個。】
      ACTION_VIEW: (android.intent.action.VIEW) 顯示指定資料。
      ACTION_EDIT: (android.intent.action.EDIT) 編輯指定資料。
      ACTION_DIAL: (android.intent.action.DIAL) 顯示撥号面闆。
      ACTION_CALL: (android.intent.action.CALL) 直接呼叫Data中所帶的号碼。
      ACTION_ANSWER: (android.intent.action.ANSWER) 接聽來電。
      ACTION_SEND: (android.intent.action.SEND) 向其他人發送資料(例如:彩信/email)。
      ACTION_SENDTO:  (android.intent.action.SENDTO) 向其他人發送短信。
      ACTION_SEARCH: (android.intent.action.SEARCH) 執行搜尋。
      ACTION_GET_CONTENT: (android.intent.action.GET_CONTENT) 讓使用者選擇資料,并傳回所選資料。      

(三)、Category 屬性:

Category屬性為Action增加額外的附加類别資訊。CATEGORY_LAUNCHER意味着在加載程式的時候Acticity出現在最上面,而CATEGORY_HOME表示頁面跳轉到HOME界面。

1、實作頁面跳轉到HOME界面的代碼:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGOTY_HOME);
startActivity(intent);      

2、常用Category屬性常量:

       CATEGORY_DEFAULT: (android.intent.category.DEFAULT) Android系統中預設的執行方式,按照普通Activity的執行方式執行。

      CATEGORY_HOME: (android.intent.category.HOME) 設定該元件為Home Activity。

      CATEGORY_PREFERENCE: (android.intent.category.PREFERENCE) 設定該元件為Preference。

      CATEGORY_LAUNCHER: (android.intent.category.LAUNCHER) 設定該元件為在目前應用程式啟動器中優先級最高的Activity,通常與入口ACTION_MAIN配合使用。

      CATEGORY_BROWSABLE: (android.intent.category.BROWSABLE) 設定該元件可以使用浏覽器啟動。

(四)、Data屬性:

  1、Data屬性通常用于向Action屬性提供操作的資料。Data屬性的值是個Uri對象。

        Uri的格式如下:scheme://host:port/path

    2、系統内置的幾個Data屬性常量:   

tel://:号碼資料格式,後跟電話号碼。
      mailto://:郵件資料格式,後跟郵件收件人位址。
      smsto://:短息資料格式,後跟短信接收号碼。
      content://:内容資料格式,後跟需要讀取的内容。
      file://:檔案資料格式,後跟檔案路徑。
      market://search?q=pname:pkgname:市場資料格式,在Google Market裡搜尋包名為pkgname的應用。
      geo://latitude, longitude:經緯資料格式,在地圖上顯示經緯度所指定的位置。      

    3、Intent利用Action屬性和Data屬性啟動Android系統内置元件的代碼:【不需要記憶,用到的時候查找資料】(一)、撥打電話:

Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);  
//intent.setAction("android.intent.action.CALL");  //以下各項皆如此,都有兩種寫法。
intent.setData(Uri.parse("tel:1320010001"));
startActivity(intent);

//調用撥号面闆:
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:1320010001"));
startActivity(intent);

//調用撥号面闆:
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("tel:1320010001"));
startActivity(intent);      

(二)、利用Uri打開浏覽器、打開地圖等:

Uri uri = Uri.parse("http://www.google.com"); //浏覽器
Uri uri=Uri.parse("geo:39.899533,116.036476"); //打開地圖定位
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);      

(五)、Type屬性:

    1、Type屬性用于指定Data所指定的Uri對應的MIME類型。MIME隻要符合“abc/xyz”這樣的字元串格式即可。

    2、 Intent利用Action、Data和Type屬性啟動Android系統内置元件的代碼:

(三)、播放視訊:

Intent intent = new Intent();
Uri uri = Uri.parse("file:///sdcard/media.mp4");
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);      

(六)、Extra屬性:

    1、通過intent.putExtra(鍵, 值)的形式在多個Activity之間進行資料交換。

    2、系統内置的幾個Extra常量:

      EXTRA_BCC:存放郵件密送人位址的字元串數組。

      EXTRA_CC:存放郵件抄送人位址的字元串數組。

      EXTRA_EMAIL:存放郵件位址的字元串數組。

      EXTRA_SUBJECT:存放郵件主題字元串。

      EXTRA_TEXT:存放郵件内容。

      EXTRA_KEY_EVENT:以KeyEvent對象方式存放觸發Intent的按鍵。

      EXTRA_PHONE_NUMBER:存放調用ACTION_CALL時的電話号碼。

    3、 Intent利用Action、Data和Type、Extra屬性啟動Android系統内置元件的代碼:

(四)、調用發送短信的程式

Intent  intent  = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("sms_body", "資訊内容...");
startActivity(intent);

//發送短資訊
Uri uri = Uri.parse("smsto:13200100001");
Intent  intent  = new Intent();
intent.setAction(Intent.  ACTION_SENDTO );
intent.setData(uri);
intent.putExtra("sms_body", "資訊内容...");
startActivity( intent );

//發送彩信,裝置會提示選擇合适的程式發送
Uri uri = Uri.parse("content://media/external/images/media/23"); //裝置中的資源(圖像或其他資源)
Intent intent = new Intent();
intent.setAction(Intent.  ACTION_SEND );
intent.setType("image/png");
intent.putExtra("sms_body", "内容");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(it);      

(五)、發送Email:

Intent intent=new Intent();
intent.setAction(Intent.  ACTION_SEND );
String[] tos={"[email protected]"};
String[] ccs={"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));


    Intent intent = new Intent(Intent.ACTION_SEND);
    String[] tos = { "[email protected]" };
    intent.putExtra(Intent.EXTRA_EMAIL, tos);
    intent.putExtra(Intent.EXTRA_TEXT, getPhoneParameter());
    intent.putExtra(Intent.EXTRA_SUBJECT, "Android日志");
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cacheDir));
    intent.setType("message/rfc882");
    intent.setType("plain/text");
    Intent.createChooser(intent, "請選擇郵件發送軟體");
    startActivity(intent);  


intent.setAction(android.provider.Settings.ACTION_SETTINGS);      

 4、 Intent利用Action屬性中的ACTION_GET_CONTENT擷取傳回值:

//選擇圖檔 requestCode 傳回的辨別
Intent intent = new Intent();
intent.setAction(Intent. ACTION_GET_CONTENT );
intent.setType( "image/*" );
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);  


//添加音頻
Intent intent = new Intent();
intent.setAction(Intent. ACTION_GET_CONTENT );
intent.setType( "video/*" );
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);  


//視訊
Intent intent = new Intent();
intent.setAction(Intent. ACTION_GET_CONTENT );
intent.setType( "video/*" );
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);  


//錄音
Intent intent = new Intent();
intent.setAction(Intent. ACTION_GET_CONTENT );
intent.setType( "audio/amr" );
intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder");
startActivityForResult(intent, requestCode);      

(七)、Flags屬性:Intent可調用addFlags()方法來為Intent添加控制标記。

    1、FLAG_ACTIVITY_CLEAR_TOP:(效果同Activity  LaunchMode的singleTask)

        如果在棧中已經有該Activity的執行個體,就重用該執行個體。重用時,會讓該執行個體回到棧頂,是以在它上面的執行個體将會被移除棧。如果棧中不存在該執行個體,将會建立新的執行個體放入棧中。

    2、FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity  LaunchMode的singleTop)

        如果在任務的棧頂正好存在該Activity的執行個體, 就重用該執行個體,而不會建立新的Activity對象。

    3、FLAG_ACTIVITY_NEW_TASK: (效果類似Activity  LaunchMode的singleInstance)

【備注:】以下幾個為了解。

    4、FLAG_ACTIVITY_MULTIPLE_TASK:

    5、FLAG_ACTIVITY_BROUGHT_TO_FRONT:

    6、FLAG_ACTIVITY_RESET_TASK_IF_NEEDED:

示例代碼:

Intent intent = new Intent(this, MainActivity.class);

//将Activity棧中處于MainActivity首頁面之上的Activity都彈出。

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

【備注:】

例如:

如果依次啟動了四個Activity:A、B、C、D。

在D Activity裡,跳到B Activity,同時希望D 和 C 都finish掉,可以在startActivity(intent)裡的intent裡添加flags标記,如下所示:

Intent intent = new Intent(this, B.class);   

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  

startActivity(intent);

這樣啟動B Activity的同時,就會把D、C都finished掉。

如果B Activity的launchMode是預設的“standard”,則B Activity會首先finished掉舊的B頁面,再啟動一個新的Activity B。  如果不想重新再建立一個新的B Activity,而是重用之前的B Activity,可以将B Activity的launchMode設定為“singleTask”。【特别需要注意的是:在部分手機中,如三星手機。即便是singleTask也會産生新的頁面,而不是重用之前的頁面。】

Activity的launchMode【知識點回顧】:

   1、standard: (備注:standard是系統預設的啟動模式。)

        标準啟動模式,每次激活Activity時都會建立Activity,并放入任務棧中。

        如果啟動此Activity的Intent中沒有設定FLAG_ACTIVITY_NEW_TASK标志, 則這個Activity與啟動他的Activity在同一個Task中,如果設定了Activity請參考上面FLAG_ACTIVITY_NEW_TASK的詳細說明,"launchMode"設定為"standard"的Activity可以被執行個體化多次, 可以在Task中的任何位置, 對于一個新的Intent請求就會執行個體化一次.

   2、singleTop:

        如果在任務的棧頂正好存在該Activity的執行個體, 就重用該執行個體,而不會建立新的Activity對象,不過它會調用onNewIntent()方法。如果棧頂部不存在就會建立新的執行個體并放入棧頂(即使棧中已經存在該Activity執行個體,隻要不在棧頂,都會建立執行個體)。

        如果啟動此Activity的Intent中沒有設定FLAG_ACTIVITY_NEW_TASK标志, 則這個Activity與啟動他的Activity在同一個Task中,如果設定了Activity請參考上面FLAG_ACTIVITY_NEW_TASK的詳細說明,"launchMode"設定為"singleTop"的Activity可以被執行個體化多次,  可以在Task中的任何位置, 對于一個新的Intent請求如果在Task棧頂, 則會用棧頂的Activity響影Intent請求,而不會重新執行個體化對象接收請求, 如果沒有在棧頂, 則會執行個體化一個新的對象接收Intent請求.

   3、singleTask:

        如果在棧中已經有該Activity的執行個體,就重用該執行個體(會調用執行個體的onNewIntent())。重用時,會讓該執行個體回到棧頂,是以在它上面的執行個體将會被移除棧。如果棧中不存在該執行個體,将會建立新的執行個體放入棧中。

        和singleTop在名字上即可看出差別,即singleTop每次隻檢測目前棧頂的Activity是否是我們需要請求建立的,而singleTask則會檢測棧中全部的Activity對象,從上向下,如果檢測到是我們所請求的則會消滅此Activity對象上面的對象,直接把檢測到的我們需要的Activity置為棧頂。

        "launchMode"設定為"singleTask"的Activity總是在棧底, 隻能被執行個體化一次, 它允許其它Activity壓入"singleTask"的Activity所在的Task棧,如果有新的Intent請求有此标志的Activity, 則系統會清除有此标志的Task棧中的全部Activity,并把此Activity顯示出來.

   4、singleInstance:

        在一個新棧中建立該Activity執行個體,并讓多個應用共享該Activity執行個體。一旦這種模式的Activity執行個體存在于某個棧中,任何應用再激活這個Activity時都會重用該棧中的執行個體,其效果相當于多個應用程式共享一個應用,不管誰激活該Activity都會進入同一個應用中。此啟動模式和我們使用的浏覽器工作原理類似,在多個程式中通路浏覽器時,如果目前浏覽器沒有打開,則打開浏覽器,否則會在目前打開的浏覽器中通路。此模式會節省大量的系統資源,因為他能保證要請求的Activity對象在目前的棧中隻存在一個。

         "launchMode"設定為"singleInstance"的Activity總是在棧底, 隻能被執行個體化一次, 不允許其它的Activity壓入"singleInstance"的Activity所在Task棧,  即整個Task棧中隻能有這麼一個Activity.

五、利用Intent屬性調用系統app的示例代碼:

1、布局核心代碼:

<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical">

            <Button

      android:id="@+id/button_main_call"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="直接撥号"/>

    <Button

        android:id="@+id/button_main_dial"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="啟動撥号面闆"/>

    <Button

        android:id="@+id/button_main_dialer"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="顯示撥号面闆"/>

    <Button

        android:id="@+id/button_main_sms"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="發送短信"/>

    <Button

        android:id="@+id/button_main_setting"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="系統設定"/>

<Button

        android:id="@+id/button_main_datesetting"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="日期設定"/>

<Button

        android:id="@+id/button_main_soundsetting"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="聲音設定"/>

<Button

        android:id="@+id/button_main_wifisetting"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="WIFI設定"/>

    <Button

        android:id="@+id/button_main_web"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="浏覽網頁"/>

    <Button

        android:id="@+id/button_main_contacts"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="檢視聯系人"/>

<Button

        android:id="@+id/button_main_showimage"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="檢視圖檔"/>

<Button

        android:id="@+id/button_main_showtext"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="檢視文本"/>

<Button

        android:id="@+id/button_main_playvideo"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="播放視訊"/>

    <Button

        android:id="@+id/button_main_playaudio"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="播放音頻"/>

<Button

        android:id="@+id/button_main_home"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick="clickButton"

        android:text="HOME"/>

      

        </LinearLayout>

    </ScrollView>      
publicclass MainActivity extends Activity {


@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}


publicvoid clickButton(View view) {

Intent intent = new Intent();

intent.setAction(android.content.Intent.ACTION_VIEW);


switch (view.getId()) {

case R.id.button_main_call:

intent.setAction(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:10086"));

break;

case R.id.button_main_dial:

intent.setAction(Intent.ACTION_DIAL);

intent.setData(Uri.parse("tel:10086"));

break;

case R.id.button_main_dialer:

intent.setAction("com.android.phone.action.TOUCH_DIALER");

break;

case R.id.button_main_sms:

intent.setAction(Intent.ACTION_SENDTO);

intent.setData(Uri.parse("smsto:10086"));

intent.putExtra("sms_body", "該吃飯了,下課吧!");

break;

case R.id.button_main_setting:

intent.setAction("android.settings.SETTINGS");

break;

case R.id.button_main_datesetting:

intent.setAction("android.settings.DATE_SETTINGS");

break;

case R.id.button_main_soundsetting:

intent.setAction("android.settings.SOUND_SETTINGS");

break;

case R.id.button_main_wifisetting:

intent.setAction("android.settings.WIFI_SETTINGS");

break;

case R.id.button_main_contacts:

intent.setAction("com.android.contacts.action.LIST_CONTACTS");

break;

case R.id.button_main_web:

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setData(Uri.parse("http://www.baidu.com"));

break;

case R.id.button_main_showimage:

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(

Uri.fromFile(new File("mnt/sdcard/Download/landscape.jpg")),

"image/*");

break;

case R.id.button_main_showtext:

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(

Uri.fromFile(new File("mnt/sdcard/Download/info.txt")),

"text/*");

break;

case R.id.button_main_playaudio:

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(Uri.fromFile(new File(

"mnt/sdcard/Download/heavencity.mp3")), "audio/*");

break;

case R.id.button_main_playvideo:

intent.setAction(android.content.Intent.ACTION_VIEW);

intent.setDataAndType(

Uri.fromFile(new File("mnt/sdcard/Download/girl.3gp")),

"video/*");

break;

case R.id.button_main_home:

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.HOME");

break;

default:

break;

}

startActivity(intent);

}


@Override

publicboolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

returntrue;

}

}