如何在 Android 上实现自己的 URI 方案?


简介

当我们需要将 Android 应用程序连接到其他应用程序或网站时,可以使用 URI(统一资源标识符)进行连接。在本文中,我们将了解如何在 Android 应用程序中创建 URI 方案。

实现

我们将创建一个简单的应用程序,其中我们将显示两个 TextView。第一个 TextView 将用于显示应用程序的标题。我们将使用第二个 TextView 来显示通过 URL 传递的数据。

步骤 1:在 Android Studio 中创建一个新项目

导航到 Android Studio,如下面的屏幕截图所示。在下面的屏幕中,点击“新建项目”以创建一个新的 Android Studio 项目。

点击“新建项目”后,您将看到下面的屏幕。

在此屏幕中,我们只需选择“空活动”并点击“下一步”。点击“下一步”后,您将看到下面的屏幕。

在此屏幕中,我们只需指定项目名称。然后包名称将自动生成。

注意 - 确保选择 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>

说明:在上面的代码中,我们创建了一个作为根布局的 RelativeLayout。在此布局内,我们创建了一个 TextView,用于显示应用程序的标题。之后,我们创建了另一个 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 {
   // creating variables on below line for text view.
   private TextView msgTV;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // initializing variables on below line.
       msgTV = findViewById(R.id.idTVMessage);
       // getting the data from our intent in our uri.
       Uri uri = getIntent().getData();
       // checking if the uri is null or not.
       if (uri != null) {
           // if the uri is not null then we are getting
           // the path segments and storing it in list.
           List<String> parameters = uri.getPathSegments();
           // after that we are extracting string
           // from that parameters.
           String param = parameters.get(parameters.size() - 1);
           // on below line we are setting that string
           // to our text view which we got as params.
           msgTV.setText(param);

       }
   }
}

说明:在上面的代码中,首先我们为 TextView 创建变量。现在我们将看到 onCreate 方法。这是每个 Android 应用程序的默认方法。当应用程序视图创建时,将调用此方法。在此方法内部,我们设置内容视图,即名为 activity_main.xml 的布局文件,以从该文件设置 UI。在 onCreate 方法内部,我们使用我们在 activity_main.xml 文件中给出的 ID 初始化 TextView 变量。之后,我们为 URI 创建一个变量,然后通过 Intent 传递数据对其进行初始化。如果 uri 不为空,则在这种情况下,我们将从传递的 URL 中解析数据并将该数据设置为我们的 TextView。

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

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

输出

结论

在本文中,我们了解了如何在 Android 应用程序中实现自己的自定义 URI 方案。

更新于: 2023 年 5 月 8 日

2K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.