如何在 Android 上实现自定义 URI 方案?
简介
当我们需要将 Android 应用连接到其他应用或网站时,可以使用 URI(统一资源标识符)进行连接。本文将介绍如何为 Android 应用创建 URI 方案。
实现
我们将创建一个简单的应用,其中包含两个文本视图。第一个文本视图用于显示应用的标题。第二个文本视图用于显示通过 URL 传递的数据。
步骤 1:在 Android Studio 中创建一个新项目
打开 Android Studio,如下图所示。
在下面的屏幕中,点击"新建项目"创建一个新的 Android Studio 项目。
点击"新建项目"后,您将看到下面的屏幕。
在此屏幕中,我们只需选择"空 Activity",然后点击"下一步"。点击"下一步"后,您将看到下面的屏幕。
在此屏幕中,我们只需指定项目名称。
然后包名将自动生成。注意- 请确保选择 Java 作为语言。
指定所有详细信息后,单击"完成"以创建一个新的 Android Studio 项目。
项目创建完成后,我们将看到两个已打开的文件:activity_main.xml 和 MainActivity.java。
步骤 2:使用 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: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"
tools:context=".MainActivity">
<!-- creating a text view on below line-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:padding="4dp"
android:text="Custom URL Scheme in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- creating a text view on below line -->
<TextView
android:id="@+id/idTVMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_margin="10dp"
android:padding="4dp"
android:text="Message will appear here"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
说明:在上面的代码中,我们创建了一个根布局,它使用相对布局(Relative Layout)。在这个布局中,我们创建了一个文本视图(TextView),用于显示应用程序的标题。之后,我们创建了另一个文本视图,用于显示通过应用程序 URI 传递的数据。
步骤 3:使用 AndroidManifest.xml 文件
导航到 app>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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JavaTestApplication"
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>
<!-- creating an intent filter on below line -->
<intent-filter>
<!-- below line is to set the action to our intent to view -->
<action android:name="android.intent.action.VIEW" />
<!-- on below line we are adding a default category to our intent -->
<category android:name="android.intent.category.DEFAULT" />
<!-- on below line we are adding a category to make our app browsable -->
<category android:name="android.intent.category.BROWSABLE" />
<!-- on below line we are specifying the host name and
the scheme type from which we will be calling our app -->
<data
android:host="www.androidapplication.com"
android:scheme="https" />
</intent-filter>
<!-- below is the same filter as above just the scheme is changed to http -->
<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:host="www.androidapplication.com"
android:scheme="https" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
说明:在上面的 AndroidManifest.xml 文件代码中,我们为应用程序创建了两个自定义 Intent 过滤器。当访问特定 URI 时,这些 Intent 过滤器将被调用,从而打开我们的应用程序。在 data 标签中,我们指定了应用程序的主机名和协议。我们可以通过在浏览器中调用该主机名来打开我们的应用程序。
步骤 4:使用 MainActivity.java 文件
导航到 MainActivity.java 文件。如果该文件不可见,请在左侧面板中导航到 app>res>layout>MainActivity.java 以打开该文件。打开该文件后,将以下代码添加到其中。代码中添加了注释,以便您详细了解。
package com.example.java_test_application;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
// 下面这行代码创建了文本视图的变量。
private TextView msgTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 在下一行初始化变量。
msgTV = findViewById(R.id.idTVMessage);
// 从 Intent 中获取 URI 中的数据。
Uri uri = getIntent().getData();
// 检查 URI 是否为空。
if (uri != null) {
// 如果 URI 不为空,则获取
// 路径段并将其存储在列表中。
List<String> parameters = uri.getPathSegments();
// 之后,我们从参数中提取字符串
//
String param = parameters.get(parameters.size() - 1);
// 下面这行代码将该字符串设置到
// 我们获取到的文本视图中。
msgTV.setText(param);
}
}
}
说明:在上面的代码中,我们首先创建了文本视图的变量。接下来我们将看到 onCreate 方法。这是每个 Android 应用程序的默认方法。当应用程序视图创建时,会调用此方法。在 onCreate 方法中,我们设置内容视图,即布局文件 activity_main.xml,以便从该文件设置 UI。在 onCreate 方法中,我们使用 activity_main.xml 文件中指定的 id 初始化文本视图变量。之后,我们创建了一个 URI 变量,并通过 Intent 传递数据来初始化它。如果 URI 不为空,则我们会解析传入 URL 中的数据,并将这些数据设置到我们的文本视图中。
添加上述代码后,只需点击顶部栏中的绿色图标即可在移动设备上运行我们的应用程序。
注意 - 请确保您已连接到您的真机或模拟器。
输出
结论
在本文中,我们探讨了如何为 Android 应用程序实现自定义 URI 方案。

