Android 如何為特定副檔名設定 Intent 過濾器?


簡介

Android Intent 過濾器是 Android 作業系統的重要組成部分。它們用於允許應用程式響應某些系統事件,例如啟動 Activity 或接收廣播。除了能夠啟動 Activity 外,Intent 過濾器還可以用於過濾特定檔案型別。

特定檔案型別的 Intent 過濾器是一種過濾器,它將導致應用程式響應使用者開啟具有特定副檔名的檔案的請求。例如,當用戶在檔案管理器中點選 .txt 檔案時,Android 作業系統將查詢已安裝應用程式列表,以檢視是否有任何應用程式具有與請求的檔案型別匹配的 Intent 過濾器。如果有,它將啟動具有 Intent 過濾器的應用程式。在本文中,我們將瞭解如何為特定副檔名建立 Android Intent 過濾器。

實現

我們將建立一個簡單的應用程式,在其中顯示一個簡單的 TextView,用於顯示應用程式的標題。然後,我們將建立兩個按鈕。一個按鈕用於檢視,另一個按鈕用於傳送,點選此按鈕時,我們將開啟一個帶有自定義引數的 Intent,這些引數將透過 Intent 過濾器傳遞。

步驟 1 - 在 Android Studio 中建立一個新專案。

導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立一個新的 Android Studio 專案。

單擊“新建專案”後,您將看到下面的螢幕。

在此螢幕中,我們只需選擇“空活動”並單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。

在此螢幕中,我們只需指定專案名稱。然後包名稱將自動生成。

注意 - 確保選擇 Java 作為語言。

指定所有詳細資訊後,單擊“完成”以建立一個新的 Android Studio 專案。

建立專案後,我們將看到兩個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。

步驟 3 - 使用 activity_main.xml。

導航到 activity_main.xml。如果此檔案不可見。要開啟此檔案。在左側窗格中導航到 app>res>layout>activity_main.xml 以開啟此檔案。開啟此檔案後。將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- text view for displaying  heading of the application -->
   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_above="@id/idBtnSendMsg"
      android:layout_centerInParent="true"
      android:padding="5dp"
      android:text="Intent Filter in Android"
      android:textAlignment="center"
      android:textAllCaps="false"
      android:textColor="@color/black"
      android:textSize="18sp"
      android:textStyle="bold" />

   <!-- on below line we are creating a button to send message -->
   <Button
      android:id="@+id/idBtnSendMsg"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="20dp"
      android:text="Send Message"
      android:textAllCaps="false" />

   <!-- on below line we are creating a button for view action -->
   <Button
      android:id="@+id/idBtnView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idBtnSendMsg"
      android:layout_centerInParent="true"
      android:layout_margin="20dp"
      android:text="View Action"
      android:textAllCaps="false" />
</RelativeLayout>

說明 - 在上面的程式碼中,我們建立了一個 Relative Layout 作為根佈局,並在其中建立了一個簡單的 TextView,用於顯示應用程式的標題。之後,我們建立了兩個按鈕,一個按鈕用於傳送操作,另一個按鈕用於檢視操作。

步驟 4 - 使用 AndroidManifest.xml 檔案。

導航到 AndroidManifest.xml 檔案並將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。

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

   <application
      android:allowBackup="true"
      android:dataExtractionRules="@xml/data_extraction_rules"
      android:fullBackupContent="@xml/backup_rules"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:preserveLegacyExternalStorage="true"
      android:requestLegacyExternalStorage="true"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/Theme.AndroidJAVAApp"
      android:usesCleartextTraffic="true"
      tools:targetApi="31">


      <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>

         <!-- on below line creating an intent filter for send action -->
         <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
         </intent-filter>

         <!-- on below line creating an intent filter for view action -->
         <intent-filter>
            <action android:name="android.intent.action.VIEW" />

               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE" />

            <data android:scheme="http" />
         </intent-filter>
      </activity>
   </application>

</manifest>

步驟 5 - 使用 MainActivity.java 檔案。

導航到 MainActivity.java。如果此檔案不可見。要開啟此檔案。在左側窗格中導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後。將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。

package com.example.androidjavaapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line creating a variable for button.
   private Button sendBtn, viewBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // on below line we are initializing variables.
      sendBtn = findViewById(R.id.idBtnSendMsg);
      viewBtn = findViewById(R.id.idBtnView);
      
      // on below line we are adding click listener for send button.
      sendBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line we are creating an intent for send action.
            Intent i = new Intent(Intent.ACTION_SEND);
            
            // on below line we are setting intent type and starting our activity.
            i.setType("text/plain");
            i.putExtra(Intent.EXTRA_EMAIL, "niranjantest@gmail.com");
            i.putExtra(Intent.EXTRA_SUBJECT, "This is a dummy message");
            i.putExtra(Intent.EXTRA_TEXT, "Dummy test message");
            startActivity(i);
         }
      });

      viewBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // on below line we are creating an intent for view action.
            Intent i = new Intent(Intent.ACTION_VIEW);
            startActivity(i);
         }
      });
   }
}

說明 - 在上面的程式碼中,我們首先為按鈕建立一個變數。之後,我們將看到一個 onCreate 方法。在此方法內部,我們將看到一個 setContentView 方法。此方法將從 activity_main.xml 檔案載入 UI。之後,我們從我們在 activity_main.xml 檔案中指定的 id 初始化按鈕的變數。之後,我們為按鈕添加了一個點選監聽器。在按鈕的點選監聽器內部,我們透過為每個按鈕指定兩個操作來建立一個 Intent,然後透過呼叫 startActivity 方法開啟此 Intent。

新增上述程式碼後,我們只需單擊頂部欄中的綠色圖示即可在移動裝置上執行我們的應用程式。

注意 - 確保您已連線到您的真實裝置或模擬器。

輸出

結論

在本文中,我們瞭解了 Android 中的 Intent 過濾器是什麼,以及如何在 Android 應用程式中使用這些 Intent 過濾器來開啟特定 Intent。

更新於: 2023年3月30日

982 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.