Android顯式Intent與隱式Intent的使用詳解
什么是Intent
Intent是各個(gè)組件之間信息溝通的橋梁,它用于Android各組件之間的通信,主要完成下列工作:
- 標(biāo)明本次通信請(qǐng)求從哪里來、到哪里去、要怎么走。
- 發(fā)起方攜帶本次通信需要的數(shù)據(jù)內(nèi)容,接收方從收到的意圖中解析數(shù)據(jù)。
- 發(fā)起方若想判斷接收方的處理結(jié)果,意圖就要負(fù)責(zé)讓接收方傳回應(yīng)答的數(shù)據(jù)內(nèi)容。
Intent的組成部分
一、顯式Intent和隱式Intent
1、顯式Intent
顯式Intent,直接指定來源活動(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
沒有明確指定要跳轉(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)作,常見系統(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)頁面"/> <Button android:id="@+id/btn_sms" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到短信頁面"/> <Button android:id="@+id/btn_my" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到我的頁面"/> </LinearLayout>
需要跳轉(zhuǎn)到的自定義的頁面的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)文章希望大家以后多多支持腳本之家!
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問題解決
- Android開發(fā)Intent跳轉(zhuǎn)傳遞list集合實(shí)現(xiàn)示例
- Android13?加強(qiáng)Intent?filters?的安全性
- android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算
- Android使用Intent的Action和Data屬性實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到撥打電話和發(fā)送短信界面
- Android Intent傳遞數(shù)據(jù)大小限制詳解
- Android開發(fā)中Intent.Action各種常見的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent基礎(chǔ)用法及作用詳解
相關(guān)文章
Android布局(RelativeLayout、TableLayout等)使用方法
這篇文章主要介紹了Android布局使用方法及各種屬性介紹,包括RelativeLayout、TableLayout等,感興趣的朋友可以參考一下2016-03-03android之計(jì)時(shí)器(Chronometer)的使用以及常用的方法
在Android的SDK中,為我們提供了一個(gè)計(jì)時(shí)器,這個(gè)計(jì)時(shí)器稱為Chronometer,我們可以成它為Android的一個(gè)組件,同時(shí)它也具備自己獨(dú)有的方法2013-01-01Android實(shí)現(xiàn)app分享文件到微信功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)app分享文件到微信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android自定義DigitalClock控件實(shí)現(xiàn)商品倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android DigitalClock實(shí)現(xiàn)商品倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02