How to open android app when url is clicked in browser

I need to open an Android app when a user clicks on a link with my domain name. For example, let's say my domain is abc.com and I posted this link on my Facebook page. When one of my friends (who installed my app on their device) clicks on the link (in the device browser), I should be able to open my website in a webview inside my app.

I'm not sure how to get this to work, but will the intent filters work? If so, can someone give me a piece of code to start with?

+3


source to share


2 answers


You have to define a custom Intent Filter in an action that should be triggered when the URL is clicked.

Let's say that you want to launch FirstActivity when a user clicks on the http://www.example.com/ link on a web page.

Add this to your activity in your manifest:



<activity android:name=".FirstActivity"
          android:label="FirstActivity">
  <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:host="www.example.com" android:scheme="http"></data>
  </intent-filter>
</activity>

      

When the user clicks the HTML link on http://www.example.com/ , the system will prompt you to either use the browser or your application.

+9


source


You can achieve this using URI schemes (link like myscheme: // open / chat) add this filter to your manifest for example. main activity section (include the name of your scheme):

<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:scheme="YOUR_SCHEME_NAME" />
</intent-filter>

      



In your activity, where you set the filter, you can get the URI by calling this (in onCreate):

Uri intentUri = getIntent().getData();

      

+6


source







All Articles