简体   繁体   中英

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?

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

First thing you should define your base URL inside strings file like this:

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

after that you should define an IntentFilter inside Manifest file like this:

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

So your link should be like this:

https://yourapp.me/api

when you click on this link it should open your app in the activity that you put this intent filter inside it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM