亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Android四大組件之Intent

原創(chuàng) 2016-11-11 14:15:28 594
摘要:Android基本的設(shè)計理念是鼓勵減少組件間的耦合,因此Android提供了Intent (意圖) ,Intent提供了一種通用的消息系統(tǒng),它允許在你的應(yīng)用程序與其它的應(yīng)用程序間傳遞Intent來執(zhí)行動作和產(chǎn)生事件。使用Intent可以激活A(yù)ndroid應(yīng)用的三個核心組件:活動、服務(wù)和廣播接收器(四大組件中還有一個是“內(nèi)容提供者”--Content Provider )所謂的“意圖”,不要想的太復(fù)

Android基本的設(shè)計理念是鼓勵減少組件間的耦合,因此Android提供了Intent (意圖) ,Intent提供了一種通用的消息系統(tǒng),它允許在你的應(yīng)用程序與其它的應(yīng)用程序間傳遞Intent來執(zhí)行動作和產(chǎn)生事件。使用Intent可以激活A(yù)ndroid應(yīng)用的三個核心組件:活動、服務(wù)和廣播接收器(四大組件中還有一個是“內(nèi)容提供者”--Content Provider )

所謂的“意圖”,不要想的太復(fù)雜,通俗點說就是“告訴Android系統(tǒng),你想做什么事兒...”,這是我個人的一點淺見。

Intent可以劃分成顯式意圖和隱式意圖。

顯式意圖:調(diào)用Intent.setComponent()或Intent.setClass()方法明確指定了組件名的Intent為顯式意圖,顯式意圖明確指定了Intent應(yīng)該傳遞給哪個組件。

隱式意圖:沒有明確指定組件名的Intent為隱式意圖。 Android系統(tǒng)會根據(jù)隱式意圖中設(shè)置的動作(action)、類別(category)、數(shù)據(jù)(URI和數(shù)據(jù)類型)找到最合適的組件來處理這個意圖。

在使用Android開發(fā)“電話撥打功能”的時候,我們通常會使用類似如下的代碼:

package dinglang.com.phone;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class phone extends Activity {
    /** Called when the activity is first created. */
private    EditText phoneText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //找到撥號按鈕
       Button button =(Button)this.findViewById(R.id.btnCall);
       phoneText =(EditText)this.findViewById(R.id.txtPhoneNumber);
       //給button注冊事件
       button.setOnClickListener(new ButtonLis());
    }
    //定義一個私有的內(nèi)部類,而且還是不能被繼承的。繼承至OnClickListener接口
    private final class  ButtonLis implements View.OnClickListener
    {
        
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //獲得文本框輸入的電話號碼
        String mobile =    phoneText.getText().toString();
            
            //調(diào)用系統(tǒng)底層API,啟動撥號程序去撥打這個號碼
            Intent intent =new Intent("android.intent.action.CALL",Uri.parse("tel:"+mobile));
        startActivity(intent);//內(nèi)部會自動添加anroid.intent.category.DEFAULT
        }
        
        
    }
}

其中“android.intent.action.CALL”是意圖過濾器中的“動作名稱”。

有人可能會疑問,我在“電話撥號程序”中好像沒有修改項目清單,沒有往里面添加任何內(nèi)容,好像也可以啊!因為電話撥號功能屬于Android系統(tǒng)內(nèi)置,系統(tǒng)會自動去匹配(比較特殊,所以不需要用戶手動去添加)。自動生成類似代碼:

<intent-filter> 
<action android:name="android.intent.action.CALL" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data android:scheme="tel" /> 
</intent-filter> 
<intent-filter> 
<action android:name="android.intent.action.CALL" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<data android:mimeType="vnd.android.cursor.item/phone" /> 
</intent-filter>

下面是一個小例子,Activity端的代碼:

package cn.itcast.intent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction("cn.itcast.lao.li");
                //intent.setData(Uri.parse("itcast://www.itcast.cn/person"))
                //intent.setType("image/gif");//使用setType方法后,將會清空前面的設(shè)置,所以可以使用下面這種方式
                intent.setDataAndType(Uri.parse("itcast://www.itcast.cn/person"), "image/gif");
                startActivity(intent);
            }
        });
    }
}

通常情況,我們在通過隱式意圖Intent激活其他組件時,為了匹配,需要在項目清單文件中類似代碼:

<activity android:name=".OtherActivity"  android:label="@string/app_name">
            <intent-filter>
                <action android:name="cn.itcast.lao.li" />
                <action android:name="cn.itcast.lao.zhang" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="cn.itcast.category.li" />
                <data android:scheme="itcast" android:host="www.itcast.cn" android:path="/person"/>
                <data android:mimeType="image/gif"/>
            </intent-filter>
        </activity>

現(xiàn)在應(yīng)該明白,意圖過濾器的作用了吧?


發(fā)佈手記

熱門詞條