Android Intent 过滤器是否支持特定文件扩展名?

androidmobile developmentapps/applications更新于 2026/2/6 22:52:17

简介

Android Intent 过滤器是 Android 操作系统的重要组成部分。它们用于允许应用响应某些系统事件,例如启动 Activity 或接收广播。除了能够启动 Activity 之外,Intent 过滤器还可用于过滤特定文件类型。

特定文件类型的 Intent 过滤器是指能够使应用响应用户打开特定扩展名文件的请求的过滤器。例如,当用户在文件管理器中点击 .txt 文件时,Android 操作系统会查询已安装的应用列表,查看其中是否有任何应用具有与请求的文件类型匹配的 Intent 过滤器。如果有,则会启动具有该 Intent 过滤器的应用。在本文中,我们将了解如何为特定文件扩展名创建 Android Intent 过滤器。

实现

我们将创建一个简单的应用程序,其中将显示一个简单的文本视图,该视图用于显示应用程序的标题。然后,我们将创建两个按钮。一个按钮用于查看,另一个按钮用于发送。点击此按钮后,我们将打开一个包含自定义参数的 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>

说明 - 在上面的代码中,我们创建了一个相对布局作为根布局,并在其中创建了一个简单的文本视图,用于显示应用程序的标题。之后,我们创建了两个按钮,一个按钮用于发送操作,另一个按钮用于查看操作。

步骤 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 {

   // 在下面一行中为按钮创建一个变量。
   private Button sendBtn, viewBtn;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      // 下面一行我们正在初始化变量。
      sendBtn = findViewById(R.id.idBtnSendMsg);
      viewBtn = findViewById(R.id.idBtnView);
      
      // 在下面一行我们添加了发送按钮的点击监听器。
      sendBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            
            // 下面一行我们正在创建一个发送动作的意图。
            Intent i = new Intent(Intent.ACTION_SEND);
            
            // 下面我们设置意图类型并开始我们的活动。
            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) {
            
            // 下面一行我们正在创建一个用于查看操作的意图。
            Intent i = new Intent(Intent.ACTION_VIEW);
            startActivity(i);
         }
      });
   }
}

说明 - 在上面的代码中,我们首先为按钮创建一个变量。之后,我们会看到一个 onCreate 方法。在这个方法中,我们会看到一个设置内容视图的方法。此方法将从 activity_main.xml 文件加载 UI。之后,我们根据在 activity_main.xml 文件中指定的 id 初始化按钮的变量。之后,我们为按钮添加一个点击监听器。在按钮的点击监听器中,我们通过为每个按钮指定两个操作来创建一个 Intent,然后通过调用启动 Activity 方法来打开此 Intent。

添加上述代码后,现在只需点击顶部栏中的绿色图标即可在移动设备上运行我们的应用程序。

注意 - 确保您已连接到真实设备或模拟器。

输出

结论

在上文中,我们了解了 Android 中的 Intent 过滤器是什么,以及如何在 Android 应用程序中使用这些 Intent 过滤器来打开特定的 Intent。


相关文章