<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Android 意圖示例 > 原文: [https://javatutorial.net/android-intent-example](https://javatutorial.net/android-intent-example) 本教程通過示例說明了 Android 的意圖,類型和方法。 ## Android 意圖 意圖是用于向另一個組件請求操作的對象。 目的是一種數據結構,其中包含要執行的操作的抽象描述。 它用于從其他 Android 組件請求功能。 簡而言之,意圖是做某事的意圖。 通過使用意圖,我們可以做很多事情,例如從一個活動轉到另一個活動,通過相機應用程序拍照,網絡搜索,在地圖上搜索位置等等。 這使開發人員可以輕松地重新混合不同的應用程序。 ## 意圖類型 有兩種類型的意圖,顯式和隱式意圖。 **顯式意圖** 在顯式意圖中,目標組件名稱在創建意圖時直接傳遞到意圖中。 當一個活動調用另一個活動時,通常使用顯式意圖。 例如,我們有兩個活動 - 登錄活動和主頁活動,登錄應用程序將用戶帶到主頁后,請參見下圖。 ![Explicit intent](https://img.kancloud.cn/fb/cd/fbcdc1f54ef7eaa058f179e378f16c5c_813x566.jpg) 顯式意圖 這是在登錄活動中應如何調用的代碼。 ```java Intent intent = new intent(this, homepageActivity); startActivity(intent); ``` **隱式意圖** 在顯式意圖中,目標組件名稱在創建時不會在意圖中傳遞。 Android 自行決定應在哪個應用程序的哪個組件中接收此意圖。 顯式意圖用于激活其他應用程序中的組件。 例如,如果您的應用程序需要打開手機中的聯系人(此意圖需要另一個應用程序即手機中的聯系人),那么代碼將如下所示 ```java Intent intent = new Intent(); Intent.setAction(android.content.intent.ACTION_VIEW); Intent.setDeta(ContactsContract.Contacts.intent.CONTENT_URI); startActivity(intent); ``` 因此,它將如下所示打開 Android 聯系人 ![Android contacts](https://img.kancloud.cn/09/a6/09a60a4d14913e6bb294c8df766f5f74_480x800.jpg) Android 聯系人 ## 目標方法 有用于向活動,服務和廣播接收者傳達意圖的單獨機制。 這是幾種方法的解釋 `Context.startActivity()`:此方法用于啟動新活動,目的是作為參數傳遞的。 `Context.startService()`:此方法用于啟動新服務,目的對象作為參數傳遞。 `Context.sendBroadcast()`:此方法用于將消息發送到任何廣播接收器。 意圖對象作為參數傳遞。 意圖對象有兩個主要組成部分。 **動作**:顯示要執行的動作。 它是意圖對象的必修部分。 要執行的動作可以是`ACTION_VIEW`,`ACTION_EDIT`等。 **數據**:它顯示要操作的數據。 它可以是簡單的數據類型或 URI。 ## **意圖**的示例 這是一個顯示意圖的示例。 轉到您的 Android Studio 并創建一個新項目。 本示例將在您的 Android 中打開`javatutorial.net`。 這是`activity_main.xml` ```java <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="368dp" android:layout_height="495dp" xmlns:tools="http://schemas.android.com/tools" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="202dp" android:text="Visit JavaTutorial.net" /> </RelativeLayout> ``` 這是`ActivtyMain.java` ```java package com.example.admin.intentexample; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.javatutorial.net")); startActivity(i); } }); } } ``` 這是它的樣子 ![intent example](https://img.kancloud.cn/fe/e4/fee4b53acfa69638af69949185372ee3_450x686.jpg) 意圖示例 ![Choose browser](https://img.kancloud.cn/33/d4/33d4cd4fe3f42984b8018a511b4b5bc5_449x689.jpg) 選擇瀏覽器 ![Java Tutorial](https://img.kancloud.cn/6b/55/6b5554fc87817f50c7502c044beda32f_451x688.jpg) Java 教程 您可以從[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/IntentExample.rar)下載此代碼。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看