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