簡體   English   中英

如何使用自定義鏈接打開我的 Android 應用程序?

[英]How can I open my Android app with a custom link?

我想通過 WhatsApp 和 Telegram 等鏈接打開我的 android 應用程序。

(示例) https://chat.whatsapp.com如果我單擊此鏈接並安裝了 WhatsApp,此鏈接將打開 WhatsApp 那么我該如何在我的應用程序中執行此操作?

如果您想深度鏈接您的應用。 例如:您打開一個鏈接,它應該通過您的應用程序打開。 在這種情況下,我在您的應用程序中使用了一個使用webView打開的網站。 當然,您可以自定義它。

開始在您的AndroidManifest.xml中創建一個<intent-filter>

      <application
         <activity
            <intent-filter>
                   ...
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <category android:name="android.intent.category.BROWSABLE"></category>
                   
                <data android:scheme="https"
                    android:host="yourURL"></data>
                   ...
            </intent-filter>
        </activity>
     </application>

在您的MainActivity.java中,您編寫以下代碼以獲取要在webView中設置的數據:

Uri uri = getIntent().getData();
        if (uri!=null){
            String path = uri.toString();
            
            WebView webView = (WebView)findViewById(R.id.webView);
            webView.loadUrl(path);
        }

以及您在activity_main.xml中定義的webView

                 <WebView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/webView"/>

而已。 祝你好運:干杯:)

首先,您應該在字符串文件中定義基本 URL,如下所示:

<string name="base_url" translatable="false">yourapp.me</string>

之后,您應該在 Manifest 文件中定義一個 IntentFilter,如下所示:

<activity android:name=".ExampleActivity">


<intent-filter android:label="@string/app_name">

<action android:name="android.intent.action.VIEW"/>

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

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

<data android:scheme="https" android:pathPrefix="/api" android:host="@string/base_url"/>

</intent-filter>

</activity>

所以你的鏈接應該是這樣的:

https://yourapp.me/api

當您單擊此鏈接時,它應該在您將此意圖過濾器放入其中的活動中打開您的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM