Search code examples
androidandroid-intentandroid-manifestdeep-linking

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


I Wanna open My android app by links like WhatsApp and Telegram.

(example) https://chat.whatsapp.com if I Click this link and WhatsApp is installed this link will open WhatsApp So how can I do that in my app?


Solution

  • If you want to deep link your app. For example: You open a link and it should be open through your app. In this case I use a website opened with a webView in your app. You can customize that of course.

    Start to create an <intent-filter> in your AndroidManifest.xml:

          <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>
    

    And in your MainActivity.java you write the following code to get the data to be set in the webView:

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

    And your webView defined in your activity_main.xml:

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

    That's it. Good luck! Cheers :)