給網(wǎng)站添加百度地圖百度關(guān)鍵字優(yōu)化價(jià)格
概述
在Android中,Intent是各個(gè)組件之間信息通信的橋梁,它用于Android各組件的通信。
Intent 的組成部分
一、顯式 Intent
第一種方式
Intent intent = new Intent(this, ActFinishActivity.class);startActivity(intent);
第二種方式
Intent intent = new Intent();intent.setClass(this, ActFinishActivity.class);startActivity(intent);
第三種方式
Intent intent = new Intent();ComponentName componentName = new ComponentName(this, ActFinishActivity.class);intent.setComponent(componentName);startActivity(intent);
可以看出第一、二種比較簡(jiǎn)單且相似,但是這兩種方式都需要獲取到要跳轉(zhuǎn)的類。如果要跳到其他應(yīng)用就不行了,用 ComponentName 這種方式可以解決,如下:
ComponentName componentName = new ComponentName("com.example.study_android", "com.example.study_android.ActFinishActivity");
二、隱式 Intent
隱式 Intent 沒有明確指定要跳轉(zhuǎn)的目標(biāo)活動(dòng),只給出一個(gè)動(dòng)作字符串讓系統(tǒng)自動(dòng)匹配,屬于模糊匹配。
- 常見系統(tǒng)動(dòng)作的取值說明
動(dòng)作名既可以通過 setAction 方法指定,也可以通過構(gòu)造函數(shù) Intent(String action) 直接生成意圖對(duì)象。 - 跳轉(zhuǎn)到撥號(hào)系統(tǒng)應(yīng)用
private void handleNavHidden() {String phoneNo = "12345";Intent intent = new Intent();intent.setAction(Intent.ACTION_DIAL);Uri uri = Uri.parse("tel:" + phoneNo);intent.setData(uri);startActivity(intent);}
- 跳轉(zhuǎn)到另一個(gè)APP
private void handleNavMy() {Intent intent = new Intent();intent.setAction("android.intent.action.MYAPP");intent.addCategory(Intent.CATEGORY_DEFAULT);startActivity(intent);}
在另一個(gè)APP的主Activity中添加以下代碼:
<intent-filter><action android:name="android.intent.action.MYAPP"/><category android:name="android.intent.category.DEFAULT" /></intent-filter>
三、向下一個(gè)Activity發(fā)送數(shù)據(jù)
- Intent 使用 Bundle 對(duì)象存放待傳遞的數(shù)據(jù)信息。
- Bundle 對(duì)象操作各類型數(shù)據(jù)的讀寫方法說明見下表:
通過 Bundle 傳遞
private void handleNextData() {Intent intent = new Intent(this, ActFinishActivity.class);Bundle bundle = new Bundle();long currentTimeMillis = System.currentTimeMillis();bundle.putString("request_time", Long.toString(currentTimeMillis));bundle.putString("request_content", "hello,world");intent.putExtras(bundle);startActivity(intent);}
在跳轉(zhuǎn)后的頁面中接收 Bundle
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_act_finish);tv_desc=findViewById(R.id.tv_desc);Bundle bundle = getIntent().getExtras();String request_time = bundle.getString("request_time");String request_content = bundle.getString("request_content");String desc = String.format("消息是:%s,\n時(shí)間是:%s", request_content, request_time);tv_desc.setText(desc);}
也可以不通過Bundle,直接傳單個(gè)數(shù)據(jù)
intent.putExtra("name","張三");
接收單個(gè)數(shù)據(jù)
String name = getIntent().getStringExtra("name");
四、向上一個(gè)Activity發(fā)送數(shù)據(jù)
步驟:
- 當(dāng)前頁面通過 registerForActivityResult 注冊(cè)回調(diào)
private ActivityResultLauncher<Intent> register;
register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {if (result != null) {Intent intent = result.getData();if (intent != null && result.getResultCode() == Activity.RESULT_OK) {Bundle bundle = intent.getExtras();String response_time = bundle.getString("response_time");String response_content = bundle.getString("response_content");String desc = String.format("返回的消息是:%s\n,時(shí)間:%s", response_content, response_time);tv_res.setText(desc);}}});
- 當(dāng)前頁面通過 register.launch 發(fā)起跳轉(zhuǎn)
private void handleDataRes() {Intent intent = new Intent(this, ActFinishActivity.class);Bundle bundle = new Bundle();long currentTimeMillis = System.currentTimeMillis();bundle.putString("request_time", Long.toString(currentTimeMillis));bundle.putString("request_content", "hello,world");bundle.putString("name", "張三");intent.putExtras(bundle);register.launch(intent);}
- 在下個(gè)頁面調(diào)用 setResult 設(shè)置數(shù)據(jù),并通過 finish結(jié)束頁面
public void onClick(View view) {Intent intent = new Intent();Bundle bundle = new Bundle();long currentTimeMillis = System.currentTimeMillis();bundle.putString("response_time", Long.toString(currentTimeMillis));bundle.putString("response_content", "你好啊");intent.putExtras(bundle);// 攜帶意圖返回上一個(gè)頁面,RESULT_OK 表示處理成功setResult(Activity.RESULT_OK, intent);// 結(jié)束當(dāng)前活動(dòng)頁finish();}
案例代碼