?
目錄
Intent 類型
一、顯式Intent方法
二、隱式Intent方法
開發(fā)文檔鏈接:Intent 和 Intent 過濾器 ?|? Android 開發(fā)者 ?|? Android Developers (google.cn)https://developer.android.google.cn/guide/components/intents-filters
Intent是一個(gè)消息傳遞對象,解決Android應(yīng)用的各項(xiàng)組件之間的通訊,您可以用來從其他應(yīng)用組件請求操作。盡管 Intent 可以通過多種方式促進(jìn)組件之間的通信,但其基本用例主要包括以下三個(gè):
- 啟動(dòng) Activity,即實(shí)現(xiàn)在幾個(gè)Activity之間的跳轉(zhuǎn)
- 啟動(dòng)服務(wù)
- 傳遞廣播
Intent 類型
Intent 分為兩種類型:
- 顯式 Intent:通過提供目標(biāo)應(yīng)用的軟件包名稱或完全限定的組件類名來指定可處理 Intent 的應(yīng)用。通常,您會(huì)在自己的應(yīng)用中使用顯式 Intent 來啟動(dòng)組件,這是因?yàn)槟酪獑?dòng)的 Activity 或服務(wù)的類名。例如,您可能會(huì)啟動(dòng)您應(yīng)用內(nèi)的新 Activity 以響應(yīng)用戶操作,或者啟動(dòng)服務(wù)以在后臺(tái)下載文件。
- 隱式 Intent?:不會(huì)指定特定的組件,而是聲明要執(zhí)行的常規(guī)操作,從而允許其他應(yīng)用中的組件來處理。例如,如需在地圖上向用戶顯示位置,則可以使用隱式 Intent,請求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。
本文主要介紹第一個(gè)用法,通過顯式和隱式分別啟動(dòng) Activity,即實(shí)現(xiàn)在幾個(gè)Activity之間的跳轉(zhuǎn)。
作好準(zhǔn)備工作,新建一個(gè)項(xiàng)目,命名為MyAppTest_1。
先來構(gòu)建兩個(gè)Activity。分別命名為MainActivity(主界面)和SecondActivity(跳轉(zhuǎn)之后的界面)。
在activity_main.xml中添加兩個(gè)按鈕。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/btn_1"
android:text="顯式Intent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<Button
android:id="@+id/btn_2"
android:text="隱式Intent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
在activity_second.xml中添加一個(gè)字段以示區(qū)分。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is SecondActivity" />
</LinearLayout>
一、顯式Intent方法
1. 1? MainActivity.java??完整代碼
package com.example.myapptest_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//跳轉(zhuǎn)頁面的按鈕,顯式Intent。
//通過findViewById獲取按鈕,并定義變量名為bt_change_1
Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
bt_change_1.setOnClickListener((View view) -> {
//顯式Intent
Intent intent = new Intent(this, SecondActivity.class);
this.startActivity(intent);
});
}
}
這是其中的第一種寫法。
//跳轉(zhuǎn)頁面的按鈕,顯式Intent。
//通過findViewById獲取按鈕,并定義變量名為bt_change_1
Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
bt_change_1.setOnClickListener((View view) -> {
//顯式Intent
Intent intent = new Intent(this, SecondActivity.class);
this.startActivity(intent);
});
還有第二種寫法,將上方代碼塊替換為如下。
//跳轉(zhuǎn)頁面的按鈕,顯式Intent。
//通過findViewById獲取按鈕,并定義變量名為bt_change_2
Button bt_change_2 =(Button)this.findViewById(R.id.btn_1);
bt_change_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
最后運(yùn)行界面及結(jié)果:
二、隱式Intent方法
隱式 Intent 指定能夠在可以執(zhí)行相應(yīng)操作的設(shè)備上調(diào)用任何應(yīng)用的操作。如果您的應(yīng)用無法執(zhí)行該操作而其他應(yīng)用可以,且您希望用戶選取要使用的應(yīng)用,則使用隱式 Intent 非常有用。
Q:? 那么是怎樣正確找到并打開目標(biāo)活動(dòng)呢?
Intent的發(fā)送者在構(gòu)造Intent對象時(shí),并不需要指定“接收者”,而是通過一定的設(shè)置,由系統(tǒng)進(jìn)行篩選,這有助于解耦。隱式Intent需要借助Intent Filter來實(shí)現(xiàn)“篩選”這一過程,并且僅當(dāng)隱式 Intent 可以通過 Intent 過濾器之一傳遞時(shí),系統(tǒng)才會(huì)將該 Intent 傳遞給應(yīng)用組件。
?
意思就是說,當(dāng)且僅當(dāng)該 “Intent對象” 與 “在清單文件中為目標(biāo)活動(dòng)創(chuàng)建的action和category” 相一致時(shí),即調(diào)用該目標(biāo)活動(dòng)。
<action>為目標(biāo)活動(dòng)地址,而<category>標(biāo)簽則包含了一些附加信息,更精確地指明了當(dāng)前的活動(dòng)能夠響應(yīng)的Intent中還可能帶有的category。只有<action>和<category>中的內(nèi)容同時(shí)能夠匹配上
Intent中指定的action和 category時(shí),這個(gè)活動(dòng)才能響應(yīng)該Intent。
?
下面是具體的做法。
由此引出,我們要注意,在使用隱式調(diào)用時(shí),需要先在AndroidManifest.xml(清單文件)中對需要打開的Activity進(jìn)行定義。
2.1?AndroidManifest.xml 完整代碼
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapptest_1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyAppTest_1">
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.myapptest_1.ACTION_START"></action>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.myapptest_1.MY_CATEGORY"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我們做了什么呢,其他的都沒有變,僅添加了以下代碼:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.myapptest_1.ACTION_START"></action>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.myapptest_1.MY_CATEGORY"/>
</intent-filter>
</activity>
幾個(gè)問題:
- 為什么在<action android:name="com.example.myapptest_1.ACTION_START"></action>這里,最后這里是有一個(gè)</action>的方式結(jié)尾,我也嘗試了以下,改為以 />結(jié)尾程序也是可以跑通的。不知道是不是有什么特別的區(qū)別。
- 這里的com.example.myapptest_1.是活動(dòng)所在的地址。需要換成你對應(yīng)的路徑。
- 為什么定義了兩個(gè) category,其中android.intent.category.DEFAULT是一種默認(rèn)的category, 在調(diào)用startActivity()方法的時(shí)候會(huì)自動(dòng)將這個(gè)category添加到 Intent中。第二個(gè)是自己定義的。
- 注意:如果當(dāng)前組件是Activity,并且沒有指定的category,必須加上category并使用默認(rèn)的DEFAULT?。?!
<category android:name="android.intent.category.DEFAULT" />
- 在<action>和<category>后面這些全大寫的指令是什么意思,這些,可以搜一下對應(yīng)的源碼。只需要跳轉(zhuǎn)頁面的話,就按照這個(gè)樣子寫是可以的。
- 注意:
- 一個(gè)Activity可以指定多個(gè)action屬性
- 一個(gè)Activity也可以指定多個(gè)category屬性
- 一個(gè)Intent只能指定一個(gè)action
- 一個(gè)intent可以指定多個(gè)category
2. 2? MainActivity.java??完整代碼
package com.example.myapptest_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//跳轉(zhuǎn)頁面的按鈕,隱性Intent
Button bt_second =(Button)this.findViewById(R.id.btn_2);
bt_second.setOnClickListener((View view) -> {
//隱性Intent
Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
intent.addCategory("com.example.myapptest_1.MY_CATEGORY");
this.startActivity(intent);
});
}
}
幾個(gè)問題:
若沒有指定的category,則不需要添加
intent.addCategory("com.example.myapptest_1.MY_CATEGORY");
只需要如下指定action即可,就是說這樣寫也是可行的,不會(huì)報(bào)錯(cuò),但不建議。
//跳轉(zhuǎn)頁面的按鈕,隱性Intent
Button bt_second =(Button)this.findViewById(R.id.btn_2);
bt_second.setOnClickListener((View view) -> {
//隱性Intent
Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
this.startActivity(intent);
});
那么我們跳轉(zhuǎn)到了新的界面怎么返回呢?
下面是返回代碼示例,我們在 SecondActivity.java 中編寫。
package com.example.myapptest_1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button gobackBtn = findViewById(R.id.button_back);
gobackBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 返回上一個(gè)頁面
finish();
}
});
}
}
相對應(yīng)的activity_second.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is SecondActivity" />
<Button
android:id="@+id/button_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="back"/>
</LinearLayout>
界面如圖。文章來源:http://www.zghlxwxcb.cn/news/detail-417564.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-417564.html
到了這里,關(guān)于使用Intent進(jìn)行頁面之間的跳轉(zhuǎn)【Intent_1】的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!