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

Android顯式Intent與隱式Intent的使用詳解

 更新時間:2022年09月28日 10:12:21   作者:Shewyoo  
Intent的中文意思是“意圖,意向”, Intent對Android的核心和靈魂,是各組件之間的橋梁。四大組件分別為Activity 、Service、BroadcastReceiver、ContentProvider。而這四種組件是獨立的,它們之間可以互相調用,協調工作,最終組成一個真正的Android應用

什么是Intent

Intent是各個組件之間信息溝通的橋梁,它用于Android各組件之間的通信,主要完成下列工作:

  • 標明本次通信請求從哪里來、到哪里去、要怎么走。
  • 發(fā)起方攜帶本次通信需要的數據內容,接收方從收到的意圖中解析數據。
  • 發(fā)起方若想判斷接收方的處理結果,意圖就要負責讓接收方傳回應答的數據內容。

Intent的組成部分

一、顯式Intent和隱式Intent

1、顯式Intent

顯式Intent,直接指定來源活動與目標活動,屬于精確匹配,有三種構建方式:

  • 在Intent的構造函數中指定。
  • 調用意圖對象的setClass方法指定。
  • 調用意圖對象的setComponent方法指定。

(1)在Intent構造函數中指定

例:

Intent intent = new Intent(this,ActNextActivity.class)//創(chuàng)建一個目標確定的意圖

(2)調用意圖對象的setClass方法指定

例:

Intent intent = new Intent();//創(chuàng)建新意圖
intent.setClass(this,ActNextActivity.class)//設置意圖要跳轉的目標活動

(3)調用意圖對象的setComponent方法指定

例:

Intent intent = new Intent();//創(chuàng)建新意圖
//創(chuàng)建包含目標活動在內的組件名稱對象
ComponentName component = new ComponentName(this,ActNextActivity.class);
intent.setComponent(component);//設置意圖攜帶的組件信息

2、隱式Intent

沒有明確指定要跳轉的目標活動,只給出一個動作字符串讓系統(tǒng)自動匹配,屬于模糊匹配。

通常APP不希望向外部暴露活動名稱,只給出一個事先定義好的標記串,這個動作名稱標記串,可以是自己定義的動作,可以是已有的系統(tǒng)動作,常見系統(tǒng)動作取值如下:

例:

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:
                //設置意圖動作為發(fā)短信
                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="點擊以下按鈕向號碼發(fā)起請求"/>
    <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>

到此這篇關于Android顯式Intent與隱式Intent的使用詳解的文章就介紹到這了,更多相關Android Intent內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Android編程之OpenGL繪圖技巧總結

    Android編程之OpenGL繪圖技巧總結

    這篇文章主要介紹了Android編程之OpenGL繪圖技巧,結合實例形式總結分析了Android基于OpenGL繪圖的原理與具體步驟,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android布局(RelativeLayout、TableLayout等)使用方法

    Android布局(RelativeLayout、TableLayout等)使用方法

    這篇文章主要介紹了Android布局使用方法及各種屬性介紹,包括RelativeLayout、TableLayout等,感興趣的朋友可以參考一下
    2016-03-03
  • android之計時器(Chronometer)的使用以及常用的方法

    android之計時器(Chronometer)的使用以及常用的方法

    在Android的SDK中,為我們提供了一個計時器,這個計時器稱為Chronometer,我們可以成它為Android的一個組件,同時它也具備自己獨有的方法
    2013-01-01
  • Android實現app分享文件到微信功能

    Android實現app分享文件到微信功能

    這篇文章主要為大家詳細介紹了Android實現app分享文件到微信功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android設計模式之策略模式詳解

    Android設計模式之策略模式詳解

    這篇文章主要為大家詳細介紹了Android設計模式之策略模式的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android中的Fragment類使用進階

    Android中的Fragment類使用進階

    這篇文章主要介紹了Android中的Fragment類使用進階,重點講解了Fragment與Activity的交互以及Fragment間的數據傳遞,需要的朋友可以參考下
    2016-04-04
  • Android實現網絡圖片瀏覽器

    Android實現網絡圖片瀏覽器

    這篇文章主要為大家詳細介紹了Android實現網絡圖片瀏覽器的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android Toast自定義顯示時間

    Android Toast自定義顯示時間

    這篇文章主要為大家詳細介紹了Android Toast自定義顯示時間,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android自定義DigitalClock控件實現商品倒計時

    Android自定義DigitalClock控件實現商品倒計時

    這篇文章主要為大家詳細介紹了Android DigitalClock實現商品倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Android 操作excel功能實例代碼

    Android 操作excel功能實例代碼

    這篇文章主要介紹了Android 操作excel功能實例代碼 ,需要的朋友可以參考下
    2017-01-01

最新評論