Intent 意圖

1.Intent做用

Intent是一個將要執行的動做的抽象的描述,解決Android應用的各項組件之間的通信,Intent負責對應用中一次操做的動做、動做涉及數據、附加數據進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。intent主要用來啓動activity或者service(並攜帶須要傳遞的參數信息)。java

2.Intent形式

意圖包括:Action(動做),Category(附加信息),Data(數據,具體內容),Tpye(類型)等等,舉個例子,說白了意圖就是啓動一個組件的的完整的動做信息,就像打人,打就是Action動做,人就是Data內容,而Type就是類型,打什麼人呢?打壞人,type就是壞指的類型,只有這些信息全了才能執行一個完整的意圖,固然還有一些信息,好比scheme就是URI類型的數據的前綴,就像這個例子當中的sms:,還有host主機名,path路徑等。android

(1).顯示意圖(Explicit Intents)express

  • 調用Intent.setComponent()或Intent.setClass()方法明確指定了組件名的Intent爲顯式意圖,顯式意圖明確指定了Intent應該傳遞給哪一個組件。
//  1.建立Intent實例化對象幾種方式  
  
Intent intent = new Intent();  
intent.setClass(Context packageContext, Class<?> cls) ;           //內部調用setComponent(ComponentName)  
intent.setClassName(Context packageContext, String className) ; //內部調用setComponent(ComponentName)  
intent.setClassName(String packageName, String className) ;     //內部調用setComponent(ComponentName),能夠激活外部應用  
  
intent.setComponent(new ComponentName(this, Class<?> cls));  
intent.setComponent(new ComponentName(this, "package Name"));

 

(2).隱式意圖(Implicit Intents)app

  • 沒有明確指定組件名的Intent爲隱式意圖。系統會根據隱式意圖中設置的 動做(action)、類別(category)、數據URI等來匹配最合適的組件。
  • 1).action:
    • The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 包括Android 系統指定的 和 自定義的
intent.setAction("com.baidu.action.TEST");

 

<action android:name="com.baidu.action.TEST"/>

 

  • 2).data
    • expressed as a Uri, The data to operate on, such as a person record in the contacts database.
Action Data(Uri) Content
ACTION_VIEW content://contacts/people/1 Display information about the person whose identifier is "1".
ACTION_VIEW tel:123 Display the phone dialer with the given number filled in.
ACTION_DIAL tel:123 Display the phone dialer with the given number filled in.
intent.setData(Uri.parse("baidu://www.baidu.com/news"));
<!-- android:path 內容字符串須要以 / 開頭 -->  
<data android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>

 

  • 3).category
    • Gives additional information about the action to execute.
    • 注意:項目清單的xml文件意圖過濾器中必須指定 android.intent.category.DEFAULT類別,Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity,or Context can't the acitivity component
intent.addCategory("com.baidu.category.TEST");
<!-- 必須指定CATEGORY_DEFAULT,只有這樣startActivity(intent)才能找到 -->  
<category android:name="com.baidu.category.TEST" />  
<category android:name="android.intent.category.DEFAULT" />

 

  • 4).type
    • Specifies an explicit type (a MIME type) of the intent data.
intent.setType("image/jpeg");
//或
<data android:mimeType="image/*" />

 

  • 注意:Java文件中data Uri 和 type不能同時使用各自的函數進行設定,由於使用type時會把Uri清除掉,可使用setDataAndType方法設定
intent.setDataAndType(Uri.parse("baidu://www.baidu.com/news"), "image/jpeg");
<data android:mimeType="image/*" android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>
  • (3).二者的使用區別ide

    • 顯式意圖通常在應用的內部使用,由於在應用內部已經知道了組件的名稱,直接調用就能夠了。 當一個應用要激活另外一個應用中的Activity時,只能使用隱式意圖,根據Activity配置的意圖過濾器建一個意圖,讓意圖中的各項參數的值都跟過濾器匹配,這樣就能夠激活其餘應用中的Activity。因此,隱式意圖是在應用與應用之間使用的。

3.Activity的Intent數據傳遞

  

//Activity間的數據傳遞  
//  1.直接向intent對象中傳入鍵值對(至關於Intent對象具備Map鍵值對功能)  
intent.putExtra("first", text1.getText().toString());  
intent.putExtra("second", text2.getText().toString());  
  
//  2.新建一個Bundle對象 ,想該對象中加入鍵值對,而後將該對象加入intent中  
Bundle bundle = new Bundle();  
bundle.putString("first", "zhang");  
bundle.putInt("age", 20);  
intent.putExtras(bundle);  
  
//  3.向intent中添加ArrayList集合對象  
intent.putIntegerArrayListExtra(name, value);  
intent.putIntegerArrayListExtra(name, value);     
  
//  4.intent傳遞Object對象(被傳遞的對象的類實現Parcelable接口,或者實現Serialiable接口)  
public Intent putExtra(String name, Serializable value)  
public Intent putExtra(String name, Parcelable value)
4.Activity退出的返回結果
//  1.經過startActivityForResult方式啓動一個Activity  
MainActivity.this.startActivityForResult(intent, 200);  //intent對象,和  requestCode請求碼  
  
//  2.新activity設定setResult方法,經過該方法能夠傳遞responseCode 和 Intent對象  
setResult(101, intent2);                                //responseCode響應碼 和 intent對象  
  
//  3.在MainActivity中覆寫onActivityResult方法,新activity一旦退出,就會執行該方法  
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    Toast.makeText(this, data.getStringExtra("info")+"requestCode:"+requestCode+"resultCode:"+resultCode, Toast.LENGTH_LONG).show();  
}

 

5.設置IntentFilter當前應用打開(應用多選)

<activity  
    android:name="com.example.player.MainActivity"  
    android:label="@string/app_name" >  
    <intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
  
        <category android:name="android.intent.category.LAUNCHER" />  
    </intent-filter>  
      
    <!-- 指定action, data類型爲video, category爲default,設置完成以後,當打開video文件時,本應用做爲可選打開軟件 -->  
    <intent-filter>  
        <action android:name="android.intent.action.VIEW" />  
  
        <data android:mimeType="video/*" />  
  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter>  
</activity>

 

使用隱式意圖啓動系統短信,並給指定號碼發送信息

/** 
     * 隱式意圖的方法啓動系統短信 
     *  
     * 簡單歸納就是: 意圖包括:Action(動做),Category(附加信息),Data(數據,具體內容),Tpye(類型)等等,舉個例子, 
     * 說白了意圖就是啓動一個組件的的完整的動做信息 
     * ,就像打人,打就是Action動做,人就是Data內容,而Type就是類型,打什麼人呢?打壞人,type就是壞指的類型 
     * ,只有這些信息全了才能執行一個完整的意圖 
     * ,固然還有一些信息,好比scheme就是URI類型的數據的前綴,就像這個例子當中的sms:,還有host主機名,path路徑等 
     *  
     * @param view 
     */  
    public void startOne(View view) {  
        Intent intent = new Intent();  
        intent.setAction("android.intent.action.SENDTO");// 發送信息的動做  
        intent.addCategory("android.intent.category.DEFAULT");// 附加信息  
        intent.setData(Uri.parse("sms:10086"));// 具體的數據,發送給10086  
        startActivity(intent);  
    }

  

  • 自定義的隱式意圖調用:
    • 首先是先在清單文件中進行註冊:
<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="net.loonggg.intent"  
    android:versionCode="1"  
    android:versionName="1.0" >  
  
    <uses-sdk  
        android:minSdkVersion="8"  
        android:targetSdkVersion="17" />  
  
    <application  
        android:allowBackup="true"  
        android:icon="@drawable/ic_launcher"  
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >  
        <activity  
            android:name="net.loonggg.intent.MainActivity"  
            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="net.loonggg.intent.SecondActivity" >  
            <intent-filter>  
  
                <!-- 自定義的動做 -->  
                <action android:name="net.loonggg.xxx" />  
                <!-- 自定義的scheme和host -->  
                <data  
                    android:host="www.baidu.com"  
                    android:path="/person"  
                    android:scheme="loonggg" />  
                <!-- 自定義的類型 -->  
                <data android:mimeType="person/people" />  
                <!-- 附加信息 -->  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </activity>  
    </application>  
  
</manifest>

 

  • 自定義設置的Activity:

  

<activity android:name="net.loonggg.intent.SecondActivity" >  
           <intent-filter>  
  
               <!-- 自定義的動做 -->  
               <action android:name="net.loonggg.xxx" />  
               <!-- 自定義的scheme和host -->  
               <data  
                   android:host="www.baidu.com"  
                   android:path="/person"  
                   android:scheme="loonggg" />  
               <!-- 自定義的類型 -->  
               <data android:mimeType="person/people" />  
               <!-- 附加信息 -->  
               <category android:name="android.intent.category.DEFAULT" />  
           </intent-filter>  
       </activity>

 

  • 代碼調用自定義
/** 
     * 經過自定義的隱式意圖啓動 
     *  
     * @param view 
     */  
    public void startTwo(View view) {  
        Intent intent = new Intent();  
        intent.setAction("net.loonggg.xxx");  
        intent.addCategory("android.intent.category.DEFAULT");  
        intent.setDataAndType(Uri.parse("loonggg://www.baidu.com/person"),  
                "person/people");  
        startActivity(intent);  
    }

 

注意:函數

intent.setData(data)和intent.setType(type)注意這兩個方法會互相清除,意思就是:若是先設置setData(data)後設置setType(type),那麼後設置的setType(type)會把前面setData(data)設置的內容清除掉,並且會報錯,反之同樣,因此若是既要設置類型與數據,那麼使用setDataAndType(data,type)這個方法。this

相關文章
相關標籤/搜索