網站首頁 編程語言 正文
什么是Intent
Intent是各個組件之間信息溝通的橋梁,它用于Android各組件之間的通信,主要完成下列工作:
- 標明本次通信請求從哪里來、到哪里去、要怎么走。
- 發起方攜帶本次通信需要的數據內容,接收方從收到的意圖中解析數據。
- 發起方若想判斷接收方的處理結果,意圖就要負責讓接收方傳回應答的數據內容。
Intent的組成部分
一、顯式Intent和隱式Intent
1、顯式Intent
顯式Intent,直接指定來源活動與目標活動,屬于精確匹配,有三種構建方式:
- 在Intent的構造函數中指定。
- 調用意圖對象的setClass方法指定。
- 調用意圖對象的setComponent方法指定。
(1)在Intent構造函數中指定
例:
Intent intent = new Intent(this,ActNextActivity.class)//創建一個目標確定的意圖
(2)調用意圖對象的setClass方法指定
例:
Intent intent = new Intent();//創建新意圖
intent.setClass(this,ActNextActivity.class)//設置意圖要跳轉的目標活動
(3)調用意圖對象的setComponent方法指定
例:
Intent intent = new Intent();//創建新意圖
//創建包含目標活動在內的組件名稱對象
ComponentName component = new ComponentName(this,ActNextActivity.class);
intent.setComponent(component);//設置意圖攜帶的組件信息
2、隱式Intent
沒有明確指定要跳轉的目標活動,只給出一個動作字符串讓系統自動匹配,屬于模糊匹配。
通常APP不希望向外部暴露活動名稱,只給出一個事先定義好的標記串,這個動作名稱標記串,可以是自己定義的動作,可以是已有的系統動作,常見系統動作取值如下:
例:
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:
//設置意圖動作為準備撥號
intent.setAction(Intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:"+phoneNo);
intent.setData(uri);
startActivity(intent);
break;
case R.id.btn_sms:
//設置意圖動作為發短信
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="點擊以下按鈕向號碼發起請求"/> <Button android:id="@+id/btn_dial" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳到撥號頁面"/> <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>
需要跳轉到的自定義的頁面的AndroidManifest.xml文件
<activity android:name=".ButtonClickActivity" android:exported="true">//需要設置為true,意為允許其他應用跳轉 <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>
原文鏈接:https://blog.csdn.net/Tir_zhang/article/details/126937291
相關推薦
- 2023-01-01 Golang反射修改變量值的操作代碼_Golang
- 2022-11-23 Pandas?DataFrame操作數據增刪查改_python
- 2022-03-15 GO + React + Axios Response to preflight request
- 2022-06-29 python密碼學庫pynacl功能介紹_python
- 2022-04-25 基于Matplotlib?調用?pyplot?模塊中?figure()?函數處理?figure圖形對
- 2022-08-31 C語言數據結構之單鏈表與雙鏈表的增刪改查操作實現_C 語言
- 2021-12-08 linux中grub啟動引導程序的加密介紹_Linux
- 2022-05-17 SQL?Server實現分頁方法介紹_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支