如何在 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+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告