Android Intent基礎(chǔ)用法及作用詳解
使用Intent在活動(dòng)中穿梭
Intent(意圖)是一種重要的消息傳遞對(duì)象,用于在不同組件(如活動(dòng)(Activity)、服務(wù)(Service)、廣播接收器(BroadcastReceiver)等)之間進(jìn)行通信和交互。
組成
Intent 主要由以下幾個(gè)部分組成:
- Action:表示要執(zhí)行的操作,比如
ACTION_VIEW
、ACTION_SEND
等。 - Data:表示要操作的數(shù)據(jù),通常是一個(gè) URI。
- Category:表示 Intent 的類(lèi)型,比如
CATEGORY_LAUNCHER
、CATEGORY_DEFAULT
等。 - Extras:表示附加的信息,可以通過(guò)
putExtra()
方法添加鍵值對(duì)。
顯式Intent
用于在應(yīng)用內(nèi)部啟動(dòng)組件(如Activity、Service、BroadcastReceiver)。顯式Intent會(huì)指定要啟動(dòng)的組件的類(lèi)名,例如:
button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 創(chuàng)建一個(gè)從 MainActivity 到 SecondActivity 的 Intent Intent intent = new Intent(MainActivity.this, SecondActivity.class); // 啟動(dòng) SecondActivity startActivity(intent); } });
創(chuàng)建一個(gè) Intent
對(duì)象,用于啟動(dòng) SecondActivity
。Intent
的第一個(gè)參數(shù)是當(dāng)前的 Context
(即 MainActivity.this
),第二個(gè)參數(shù)是要啟動(dòng)的目標(biāo) Activity
類(lèi)(即 SecondActivity.class
)。
隱式Intent
用于在不指定組件名稱(chēng)的情況下啟動(dòng)組件,而是通過(guò)指定動(dòng)作(Action)、數(shù)據(jù)(Data)和類(lèi)型(Type)等信息,讓系統(tǒng)去匹配合適的組件。 啟動(dòng)activity
在標(biāo)簽中添加下面代碼:
<activity android:name=".SecondActivity" android:exported="true" android:theme="@style/Theme.AppCompat.DayNight.DialogWhenLarge"> <intent-filter> <!-- 指定動(dòng)作為 com.example.menutest_716.SecondActivity --> <action android:name="com.example.menutest_716.SecondActivity" /> <!-- 添加默認(rèn)分類(lèi) --> <category android:name="android.intent.category.DEFAULT" /> <!-- 添加自定義分類(lèi) com.example.menutest_716.MY_CATEGORY --> <category android:name="com.example.menutest_716.MY_CATEGORY" /> </intent-filter> </activity>
Button button5 = findViewById(R.id.button5); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 創(chuàng)建一個(gè)隱式 Intent,指定動(dòng)作為 com.example.menutest_716.SecondActivity Intent intent = new Intent("com.example.menutest_716.SecondActivity"); // 添加自定義分類(lèi) com.example.menutest_716.MY_CATEGORY intent.addCategory("com.example.menutest_716.MY_CATEGORY"); // 啟動(dòng)符合條件的 Activity startActivity(intent); } });
打開(kāi)網(wǎng)頁(yè)
button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 創(chuàng)建一個(gè)新的 Intent,動(dòng)作為 ACTION_VIEW Intent intent = new Intent(Intent.ACTION_VIEW); // 設(shè)置 Intent 的數(shù)據(jù)為指定的 URI,這里是打開(kāi)百度網(wǎng)站的示例 intent.setData(Uri.parse("http://www.baidu.com")); // 啟動(dòng)能夠處理此 Intent 的 Activity startActivity(intent); } });
撥打電話
button8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 創(chuàng)建一個(gè)撥號(hào)的 Intent Intent intent = new Intent(Intent.ACTION_DIAL); // 設(shè)置 Intent 的數(shù)據(jù)為要撥打的電話號(hào)碼 intent.setData(Uri.parse("tel:10086")); // 啟動(dòng)該 Intent,跳轉(zhuǎn)到系統(tǒng)的撥號(hào)界面 startActivity(intent); } });
顯式與隱式區(qū)別
- 目的:顯式Intent用于精確指定要啟動(dòng)的組件,通常在應(yīng)用內(nèi)部使用;隱式Intent用于請(qǐng)求系統(tǒng)啟動(dòng)能夠處理指定動(dòng)作或數(shù)據(jù)類(lèi)型的任何組件。
- 使用方式:顯式Intent直接指定組件類(lèi)名,而隱式Intent通過(guò)設(shè)置動(dòng)作、數(shù)據(jù)等信息間接指定要啟動(dòng)的組件,系統(tǒng)會(huì)根據(jù)匹配規(guī)則找到合適的組件處理請(qǐng)求。
作用
- 組件通信:Intent 允許應(yīng)用程序的不同組件(如活動(dòng)、服務(wù)、廣播接收器)之間進(jìn)行通信。通過(guò)發(fā)送Intent,一個(gè)組件可以請(qǐng)求另一個(gè)組件執(zhí)行特定的操作或返回結(jié)果。
- 啟動(dòng)活動(dòng):當(dāng)你需要從一個(gè)活動(dòng)跳轉(zhuǎn)到另一個(gè)活動(dòng)時(shí),可以使用Intent來(lái)啟動(dòng)目標(biāo)活動(dòng)。這使得應(yīng)用程序的導(dǎo)航更加靈活和動(dòng)態(tài)。
- 啟動(dòng)服務(wù):服務(wù)是后臺(tái)運(yùn)行的組件,通常用于執(zhí)行長(zhǎng)時(shí)間運(yùn)行的操作,如下載文件或播放音樂(lè)。通過(guò)Intent,你可以啟動(dòng)或綁定到一個(gè)服務(wù),并與之交互。
- 發(fā)送廣播:廣播是一種消息傳遞機(jī)制,允許應(yīng)用程序向系統(tǒng)或應(yīng)用程序的其他部分發(fā)送消息。Intent 可以用于發(fā)送和接收廣播,從而實(shí)現(xiàn)應(yīng)用程序之間的通信。
- 數(shù)據(jù)傳輸:Intent 可以攜帶數(shù)據(jù)(如字符串、URI、文件等),這使得數(shù)據(jù)可以在不同的組件之間傳遞。這對(duì)于實(shí)現(xiàn)復(fù)雜的交互和數(shù)據(jù)共享非常有用。
- 隱式Intent:隱式Intent不指定具體的組件,而是通過(guò)定義一個(gè)操作(action)、數(shù)據(jù)類(lèi)型(data)和類(lèi)別(category)來(lái)請(qǐng)求系統(tǒng)執(zhí)行相應(yīng)的操作。這使得應(yīng)用程序可以更靈活地與其他應(yīng)用程序或系統(tǒng)服務(wù)交互。
- 顯式Intent:顯式Intent直接指向特定的組件,如某個(gè)活動(dòng)或服務(wù)。這使得開(kāi)發(fā)者可以精確控制應(yīng)用程序的行為和交互。
活動(dòng)間傳遞數(shù)據(jù)
有兩個(gè)活動(dòng):MainActivity
和 SecondActivity
,要從 MainActivity
向 SecondActivity
傳遞數(shù)據(jù)。
在 MainActivity
中:
- 創(chuàng)建一個(gè)
Intent
對(duì)象,并使用putExtra
方法添加要傳遞的數(shù)據(jù)。 - 使用
startActivity
方法啟動(dòng)SecondActivity
。
Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String data = "祝你開(kāi)心永遠(yuǎn)"; Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("extra_data",data); startActivity(intent); } });
在 SecondActivity
中:
在 onCreate
方法中獲取傳遞過(guò)來(lái)的數(shù)據(jù)。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); // 獲取傳遞的數(shù)據(jù) Intent intent = getIntent(); String value = intent.getStringExtra("key"); }
返回?cái)?shù)據(jù)給上一個(gè)活動(dòng)
在 Android 中,如果我們需要從一個(gè)活動(dòng)返回?cái)?shù)據(jù)給上一個(gè)活動(dòng),可以使用 startActivityForResult
方法啟動(dòng)目標(biāo)活動(dòng),并在目標(biāo)活動(dòng)中設(shè)置結(jié)果數(shù)據(jù)。
在 MainActivity
中:
- 使用
startActivityForResult
方法啟動(dòng)SecondActivity
。 - 重寫(xiě)
onActivityResult
方法來(lái)接收返回的數(shù)據(jù)。
// 啟動(dòng) SecondActivity 并請(qǐng)求返回結(jié)果 Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, 1); // 重寫(xiě) onActivityResult 方法來(lái)接收返回的數(shù)據(jù) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == RESULT_OK) { // 從返回的 Intent 中獲取數(shù)據(jù) String result = data.getStringExtra("result_key"); // 處理返回的數(shù)據(jù) } }
在 SecondActivity
中:
- 創(chuàng)建一個(gè)
Intent
對(duì)象,并使用putExtra
方法添加要返回的數(shù)據(jù)。 - 使用
setResult
方法設(shè)置結(jié)果,并傳入包含數(shù)據(jù)的Intent
。 - 調(diào)用
finish
方法關(guān)閉當(dāng)前活動(dòng)。
// 創(chuàng)建一個(gè) Intent 對(duì)象并添加要返回的數(shù)據(jù) Intent returnIntent = new Intent(); returnIntent.putExtra("result_key", "result_value"); // 設(shè)置結(jié)果并傳入包含數(shù)據(jù)的 Intent setResult(RESULT_OK, returnIntent); // 關(guān)閉當(dāng)前活動(dòng) finish();
到此這篇關(guān)于Android Intent基礎(chǔ)用法及作用的文章就介紹到這了,更多相關(guān)Android Intent用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android顯式Intent與隱式Intent的使用詳解
- Android Intent傳遞大量數(shù)據(jù)出現(xiàn)問(wèn)題解決
- Android開(kāi)發(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開(kāi)發(fā)中Intent.Action各種常見(jiàn)的作用匯總
- Android使用Intent隱式實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的兩種方法
相關(guān)文章
Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢(xún)地址并對(duì)獲取的json數(shù)據(jù)進(jìn)行解析的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)根據(jù)經(jīng)緯度查詢(xún)地址并對(duì)獲取的json數(shù)據(jù)進(jìn)行解析的方法,結(jié)合實(shí)例形式分析了Android的經(jīng)緯度地址解析與json格式數(shù)據(jù)操作相關(guān)技巧,需要的朋友可以參考下2017-02-02Android Animation之TranslateAnimation(平移動(dòng)畫(huà))
這篇文章主要為大家詳細(xì)介紹了Animation之TranslateAnimation平移動(dòng)畫(huà),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09MPAndroidChart 自定義圖表繪制使用實(shí)例
這篇文章主要為大家介紹了MPAndroidChart 自定義圖表繪制使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android開(kāi)發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋
這篇文章主要介紹了Android開(kāi)發(fā)的IDE、ADT、SDK、JDK、NDK等名詞解釋,對(duì)這些概念搞不清楚是一件痛苦的事,本文就簡(jiǎn)潔講解了這些名詞的含義,一起掃盲吧,需要的朋友可以參考下2015-06-06Android 自定義輸入支付密碼的軟鍵盤(pán)實(shí)例代碼
這篇文章主要介紹了Android 自定義輸入支付密碼的軟鍵盤(pán)實(shí)例代碼的相關(guān)資料,并附簡(jiǎn)單實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)
這篇文章主要介紹了Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
本篇文章主要介紹了Android實(shí)現(xiàn)Service下載文件,Notification顯示下載進(jìn)度,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析
這篇文章主要介紹了Android操作系統(tǒng)的架構(gòu)設(shè)計(jì)分析,Android系統(tǒng)架構(gòu)分為L(zhǎng)inux內(nèi)核驅(qū)動(dòng)、C/C ++框架、Java框架、Java應(yīng)用程序,本文分別講解了它的作用,需要的朋友可以參考下2015-06-06Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置
這篇文章主要為大家詳細(xì)介紹了Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08