欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中Intent組件的入門學(xué)習(xí)心得

 更新時間:2021年12月13日 12:52:20   作者:dhkjzhr  
Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個知識,也非常的重要,下面這篇文章主要給大家介紹了關(guān)于Android中Intent組件的相關(guān)資料,需要的朋友可以參考下

什么是 Intent ?

Intent是Android開發(fā)中一個非常重要且常用的類,Intent是一個消息傳遞對象,可以用來從其他應(yīng)用組件請求操作,使用Intent可以在一個組件中同一APP中的另一個組件或者是啟動另一個APP的組件(這里所說的組件包括Activity、Service和Broadcast)。

ctivity、service和broadcast receiver之間是通過Intent進行通信的,而另外一個組件Content Provider本身就是一種通信機制,不需要通過Intent。我們來看下面這個圖就知道了:

如果Activity1需要和Activity2進行聯(lián)系,二者不需要直接聯(lián)系,而是通過Intent作為橋梁。通俗來講,Intnet類似于中介、媒婆的角色。

安卓官方對于Intent的介紹

Intent 的類型?

Intent 有兩種類型,分別是 顯式 Intent 和 隱式 Intent 。

顯式 Intent: 通過提供目標(biāo)應(yīng)用的軟件包名稱或完全限定的組件類名來指定可處理 Intent 的應(yīng)用。通常,您會在自己的應(yīng)用中使用 顯式 Intent 來啟動組件,這是因為您知道要啟動的 Activity 或服務(wù)的類名。例如,您可能會啟動您應(yīng)用內(nèi)的新 Activity 以響應(yīng)用戶操作,或者啟動服務(wù)以在后臺下載文件。

隱式 Intent: 不會指定特定的組件,而是聲明要執(zhí)行的常規(guī)操作,從而允許其他應(yīng)用中的組件來處理。例如,如需在地圖上向用戶顯示位置,則可以使用 隱式 Intent,請求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。使用 隱式 Intent 時,Android 系統(tǒng)通過將 Intent 的內(nèi)容與在設(shè)備上其他應(yīng)用的清單文件中聲明的 Intent 過濾器 進行比較,從而找到要啟動的相應(yīng)組件。如果 Intent 與 Intent 過濾器匹配,則系統(tǒng)將啟動該組件,并向其傳遞 Intent 對象。如果多個 Intent 過濾器兼容,則系統(tǒng)會顯示一個對話框,支持用戶選取要使用的應(yīng)用。

Intent 的組成

intent由組件名稱(Component name)、操作(Action)、數(shù)據(jù)(Data)、類別(Category)、額外的數(shù)據(jù)(Extra)和標(biāo)志(Flag)六個部分組成。

組件名稱 Component name:

組件名稱是要啟動的組件名稱。如果使用的是 顯式 Intent 則必須指定此參數(shù),否則 Intent 無法識別要傳遞給哪個應(yīng)用組件。不指定此參數(shù)則為 隱式 Intent ,系統(tǒng)將根據(jù)其他 Intent 信息決定要接受 Intent 的應(yīng)用組件。如果想要啟動特定的組件,則必須要指定此參數(shù)為該組件的名稱。

操作 Action:

操作指定要執(zhí)行的通用操作的字符串??梢宰远x自己的操作,但是通常應(yīng)該使用由 Intent 類或其他框架類定義的操作常量(例如 ACTION_VIEWACTION_SEND)。

數(shù)據(jù) Data:

數(shù)據(jù)是引用待操作數(shù)據(jù)或該數(shù)據(jù) MIME 類型的 URI 對象。提供的數(shù)據(jù)類型通常由 Intent 的操作決定。創(chuàng)建 Intent 時,除了指定 URI 以外,指定數(shù)據(jù)類型(其 MIME 類型)往往也很重要。

類別 Category:

類別是一個包含應(yīng)處理 Intent 組件類型的附加信息的字符串??梢詫⑷我鈹?shù)量的類別描述放入一個 Intent 中,但大多數(shù) Intent 均不需要類別。以下是一些常見類別:

CATEGORY_BROWSABLE

目標(biāo) Activity 允許本身通過網(wǎng)絡(luò)瀏覽器啟動,以顯示鏈接引用的數(shù)據(jù),如圖像或電子郵件。

CATEGORY_LAUNCHER

此類別描述了該 Activity 是任務(wù)的初始 Activity,當(dāng)系統(tǒng)啟動時首先啟動此 Activity。

額外的數(shù)據(jù) Extra:

攜帶完成請求操作所需的附加信息的鍵值對。正如某些操作使用特定類型的數(shù)據(jù) URI 一樣,有些操作也使用特定的 extra。

標(biāo)志 Flag:

標(biāo)志在 Intent 類中定義,充當(dāng) Intent 的元數(shù)據(jù)。標(biāo)志可以指示 Android 系統(tǒng)如何啟動 Activity(例如,Activity 應(yīng)屬于哪個任務(wù)),以及啟動之后如何處理(例如,Activity 是否屬于最近的 Activity 列表)。

顯式 Intent 的使用

首先新建一個 Activity 以及相應(yīng)的布局文件。

MyActivity

import androidx.annotation.Nullable;
import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myactivity);
    }
}

myactivity.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"
    android:orientation="vertical"
    tools:context=".MyActivity">
    <TextView
        android:text="這是一個新的頁面"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在清單文件 AndroidManifest.xml 中注冊 MyActivity 。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication1">

    <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.MyApplication1">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyActivity"/>
    </application>

</manifest>

此處android:name中使用 .MyActivity 是為了程序在運行時直接將該 name 添加到 package 后,這樣方便程序去尋找此Activity并啟動,也可以將package復(fù)制添加到 android:name 中,例如<activity android:name="com.example.myapplication1.MyActivity"/> 。

在 MainActivity 中設(shè)定一個按鈕 id 為 startNew ,當(dāng)按下該按鈕時啟動 MyActivity 。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,MyActivity.class));
            }
        });
    }
}

此處在 new Intent(MainActivity.this,MyActivity.class) 中指明了要啟動的 Activity 的名稱,所以為顯式 Intent 。

運行效果:

隱式 Intent 的使用

首先在清單文件 AndroidManifest 中修改 MyAcitivity 的相關(guān)定義。

<activity
    android:name=".MyActivity"
    android:exported="true">
    <intent-filter>
        //為此intent定義一個新的action名稱
        <action android:name="NewAction"/>
        //若指定category為DEFAULT,則指明此intent的行為方式是Activity
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

修改 MainActivity ,將原本的顯式 Intent 啟動方式改為隱式 Intent ,即通過自定義的 action 名稱來啟動相對應(yīng)的 Activity 。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此處使用NewAction調(diào)用MyActivity
                startActivity(new Intent("NewAction"));
            }
        });
    }
}

這樣就成功通過隱式 Intent 啟動 MyActivity 。

通常建議將 定義為 “包.intent.action.組件名” ??梢酝ㄟ^在組件中定義靜態(tài)變量來更加方便地訪問該組件。例如,public static final String ACTION = "com.example.myapplication1.intent.action.MyActivity";

調(diào)用時直接使用 MyActivity.ACTION 即可。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.startNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //此處使用NewAction調(diào)用
                startActivity(new Intent(MyActivity.ACTION));
            }
        });
    }
}

運行效果與顯式 Intent 相同。

通過隱式 Intent ,組件可以對不同APP之間的組件進行訪問。如果想讓本應(yīng)用的組件可以被其他的應(yīng)用進行訪問,則需要對 activity 標(biāo)簽加上一個屬性 android:exported ,當(dāng) android:exported 為 true 時,本組件可以被其他應(yīng)用組件訪問,為 false 時則不可被其他應(yīng)用訪問,并彈出異常警告??梢栽趩悠渌麘?yīng)用組件時加上異常捕獲語句,例如,

public void onClick(View view) {
    try {
        startActivity(new Intent(MyActivity.ACTION));
    }catch (Exception e){
        Toast.makeText(MainActivity.this,"無法啟動指定的Activity",Toast.LENGTH_SHORT).show();
    }
}

本文大部分參考了Android中對Intent部分的描述,希望本文對大家更好地使用Intent對象有所幫助。

總結(jié)

到此這篇關(guān)于Android中Intent組件入門學(xué)習(xí)的文章就介紹到這了,更多相關(guān)Android Intent組件學(xué)習(xí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • android studio 一直卡在Gradle:Build Running的幾種解決辦法

    android studio 一直卡在Gradle:Build Running的幾種解決辦法

    這篇文章主要介紹了android studio 一直卡在Gradle:Build Running的解決辦法,非常具有實用價值,需要的朋友可以參考下
    2017-10-10
  • Android錄制mp3格式文件

    Android錄制mp3格式文件

    這篇文章主要為大家詳細介紹了Android錄制mp3格式文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android利用Xfermode剪裁圓角

    Android利用Xfermode剪裁圓角

    這篇文章主要為大家詳細介紹了Android利用Xfermode剪裁圓角,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android源碼解析之屬性動畫詳解

    Android源碼解析之屬性動畫詳解

    在手機上去實現(xiàn)動畫效果算是件比較炫酷的事情,自Android 3.0版本開始,系統(tǒng)給我們提供了一種全新的動畫模式,屬性動畫(property animation),它的功能非常強大,彌補了之前補間動畫的一些缺陷,幾乎是可以完全替代掉補間動畫了。本文就詳細介紹了Android中的屬性動畫。
    2017-02-02
  • Android 更新RecyclerView的好方法

    Android 更新RecyclerView的好方法

    在使用RecyclerView的時候不免要修改RecyclerView的數(shù)據(jù),使用notifyDataSetChanged()來刷新界面,但是當(dāng)數(shù)據(jù)多,而只是修改了一點的數(shù)據(jù),或者刷新比較頻繁,這樣就會導(dǎo)致界面卡頓,用戶交互特別不好,這時可以使用RecyclerView方法解決,具體實現(xiàn)代碼大家參考下本文吧
    2017-06-06
  • Android開發(fā)之在程序中時時獲取logcat日志信息的方法(附demo源碼下載)

    Android開發(fā)之在程序中時時獲取logcat日志信息的方法(附demo源碼下載)

    這篇文章主要介紹了Android開發(fā)之在程序中時時獲取logcat日志信息的方法,結(jié)合實例形式較為詳細的分析了實時獲取logcat日志的原理、步驟與相關(guān)實現(xiàn)技巧,并附帶相應(yīng)的demo源碼供讀者下載參考,需要的朋友可以參考下
    2016-02-02
  • 詳解Flutter中數(shù)據(jù)傳遞的方式

    詳解Flutter中數(shù)據(jù)傳遞的方式

    這篇文章主要和大家分享一下Flutter中常用的幾種數(shù)據(jù)傳遞方式的應(yīng)用場景以及優(yōu)缺點,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-06-06
  • Android自定義View Material Design理念詳解

    Android自定義View Material Design理念詳解

    這篇文章主要為大家介紹了Android自定義View Material Design理念詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Android實現(xiàn)簡易的鬧鐘功能

    Android實現(xiàn)簡易的鬧鐘功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)簡易的鬧鐘功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 強制Android應(yīng)用使用某個Locale的方法

    強制Android應(yīng)用使用某個Locale的方法

    這篇文章主要介紹了強制Android應(yīng)用使用某個Locale的方法,涉及Android基于Locale進行語言設(shè)置的相關(guān)技巧,需要的朋友可以參考下
    2015-10-10

最新評論