最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android顯式Intent與隱式Intent的使用詳解

 更新時(shí)間:2022年09月28日 10:12:21   作者:Shewyoo  
Intent的中文意思是“意圖,意向”, Intent對(duì)Android的核心和靈魂,是各組件之間的橋梁。四大組件分別為Activity 、Service、BroadcastReceiver、ContentProvider。而這四種組件是獨(dú)立的,它們之間可以互相調(diào)用,協(xié)調(diào)工作,最終組成一個(gè)真正的Android應(yīng)用

什么是Intent

Intent是各個(gè)組件之間信息溝通的橋梁,它用于Android各組件之間的通信,主要完成下列工作:

  • 標(biāo)明本次通信請(qǐng)求從哪里來(lái)、到哪里去、要怎么走。
  • 發(fā)起方攜帶本次通信需要的數(shù)據(jù)內(nèi)容,接收方從收到的意圖中解析數(shù)據(jù)。
  • 發(fā)起方若想判斷接收方的處理結(jié)果,意圖就要負(fù)責(zé)讓接收方傳回應(yīng)答的數(shù)據(jù)內(nèi)容。

Intent的組成部分

一、顯式Intent和隱式Intent

1、顯式Intent

顯式Intent,直接指定來(lái)源活動(dòng)與目標(biāo)活動(dòng),屬于精確匹配,有三種構(gòu)建方式:

  • 在Intent的構(gòu)造函數(shù)中指定。
  • 調(diào)用意圖對(duì)象的setClass方法指定。
  • 調(diào)用意圖對(duì)象的setComponent方法指定。

(1)在Intent構(gòu)造函數(shù)中指定

例:

Intent intent = new Intent(this,ActNextActivity.class)//創(chuàng)建一個(gè)目標(biāo)確定的意圖

(2)調(diào)用意圖對(duì)象的setClass方法指定

例:

Intent intent = new Intent();//創(chuàng)建新意圖
intent.setClass(this,ActNextActivity.class)//設(shè)置意圖要跳轉(zhuǎn)的目標(biāo)活動(dòng)

(3)調(diào)用意圖對(duì)象的setComponent方法指定

例:

Intent intent = new Intent();//創(chuàng)建新意圖
//創(chuàng)建包含目標(biāo)活動(dòng)在內(nèi)的組件名稱對(duì)象
ComponentName component = new ComponentName(this,ActNextActivity.class);
intent.setComponent(component);//設(shè)置意圖攜帶的組件信息

2、隱式Intent

沒(méi)有明確指定要跳轉(zhuǎn)的目標(biāo)活動(dòng),只給出一個(gè)動(dòng)作字符串讓系統(tǒng)自動(dòng)匹配,屬于模糊匹配。

通常APP不希望向外部暴露活動(dòng)名稱,只給出一個(gè)事先定義好的標(biāo)記串,這個(gè)動(dòng)作名稱標(biāo)記串,可以是自己定義的動(dòng)作,可以是已有的系統(tǒng)動(dòng)作,常見(jiàn)系統(tǒng)動(dòng)作取值如下:

例:

java

public class ActionUrlActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_url);
        findViewById(R.id.btn_dial).setOnClickListener(this);
        findViewById(R.id.btn_sms).setOnClickListener(this);
        findViewById(R.id.btn_my).setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        String phoneNo = "12345";
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.btn_dial:
                //設(shè)置意圖動(dòng)作為準(zhǔn)備撥號(hào)
                intent.setAction(Intent.ACTION_DIAL);
                Uri uri = Uri.parse("tel:"+phoneNo);
                intent.setData(uri);
                startActivity(intent);
                break;
            case R.id.btn_sms:
                //設(shè)置意圖動(dòng)作為發(fā)短信
                intent.setAction(Intent.ACTION_SENDTO);
                Uri uri2 = Uri.parse("smsto:"+phoneNo);
                intent.setData(uri2);
                startActivity(intent);
                break;
            case R.id.btn_my:
                intent.setAction("android.intent.action.NING");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                startActivity(intent);
                break;
        }
    }
}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="點(diǎn)擊以下按鈕向號(hào)碼發(fā)起請(qǐng)求"/>
    <Button
        android:id="@+id/btn_dial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到撥號(hào)頁(yè)面"/>
    <Button
        android:id="@+id/btn_sms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到短信頁(yè)面"/>
    <Button
        android:id="@+id/btn_my"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到我的頁(yè)面"/>
</LinearLayout>

需要跳轉(zhuǎn)到的自定義的頁(yè)面的AndroidManifest.xml文件

        <activity
            android:name=".ButtonClickActivity"
            android:exported="true">//需要設(shè)置為true,意為允許其他應(yīng)用跳轉(zhuǎn)
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            //添加的代碼:
            <intent-filter>
                <action android:name="android.intent.action.NING" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

到此這篇關(guān)于Android顯式Intent與隱式Intent的使用詳解的文章就介紹到這了,更多相關(guān)Android Intent內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

汪清县| 嘉禾县| 博客| 重庆市| 余姚市| 同仁县| 惠安县| 治县。| 江永县| 衡山县| 海口市| 大关县| 大同县| 旅游| 霍州市| 武乡县| 商都县| 东城区| 黄大仙区| 德清县| 开封市| 长岭县| 安新县| 克拉玛依市| 库尔勒市| 鄂伦春自治旗| 华容县| 固始县| 珲春市| 青河县| 南江县| 万源市| 梅河口市| 焉耆| 邓州市| 安平县| 万宁市| 静安区| 册亨县| 鱼台县| 梁河县|