亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

什么是 Intent ?

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

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

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

安卓官方對(duì)于Intent的介紹

Intent 的類型?

Intent 有兩種類型,分別是 顯式 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,請(qǐng)求另一具有此功能的應(yīng)用在地圖上顯示指定的位置。使用 隱式 Intent 時(shí),Android 系統(tǒng)通過將 Intent 的內(nèi)容與在設(shè)備上其他應(yīng)用的清單文件中聲明的 Intent 過濾器 進(jìn)行比較,從而找到要啟動(dòng)的相應(yīng)組件。如果 Intent 與 Intent 過濾器匹配,則系統(tǒng)將啟動(dòng)該組件,并向其傳遞 Intent 對(duì)象。如果多個(gè) Intent 過濾器兼容,則系統(tǒng)會(huì)顯示一個(gè)對(duì)話框,支持用戶選取要使用的應(yīng)用。

Intent 的組成

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

組件名稱 Component name:

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

操作 Action:

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

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

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

類別 Category:

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

CATEGORY_BROWSABLE

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

CATEGORY_LAUNCHER

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

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

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

標(biāo)志 Flag:

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

顯式 Intent 的使用

首先新建一個(gè) 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="這是一個(gè)新的頁面"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在清單文件 AndroidManifest.xml 中注冊(cè) 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 是為了程序在運(yùn)行時(shí)直接將該 name 添加到 package 后,這樣方便程序去尋找此Activity并啟動(dòng),也可以將package復(fù)制添加到 android:name 中,例如<activity android:name="com.example.myapplication1.MyActivity"/> 。

在 MainActivity 中設(shè)定一個(gè)按鈕 id 為 startNew ,當(dāng)按下該按鈕時(shí)啟動(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) 中指明了要啟動(dòng)的 Activity 的名稱,所以為顯式 Intent 。

運(yùn)行效果:

隱式 Intent 的使用

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

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

修改 MainActivity ,將原本的顯式 Intent 啟動(dòng)方式改為隱式 Intent ,即通過自定義的 action 名稱來啟動(dòng)相對(duì)應(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 啟動(dòng) MyActivity 。

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

調(diào)用時(shí)直接使用 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));
            }
        });
    }
}

運(yùn)行效果與顯式 Intent 相同。

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

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

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

總結(jié)

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

相關(guān)文章

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

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

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

    Android錄制mp3格式文件

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

    Android利用Xfermode剪裁圓角

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

    Android源碼解析之屬性動(dòng)畫詳解

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

    Android 更新RecyclerView的好方法

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

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

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

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

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

    Android自定義View Material Design理念詳解

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

    Android實(shí)現(xiàn)簡(jiǎn)易的鬧鐘功能

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

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

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

最新評(píng)論