Many times we have seen that there are some URL(s) in our phone like in a email or whatsapp etc. and when clicked on them they take us to a particular application in our phone rather than to browser like there is a URL of facebook in your chat then on click on it the link is opened in Facebook app ( if you have Facebook application in your device), so i can we implement it for our own application.
So here I am having an app that should be opened when a particular URL is clicked. I made a simple one which will show the URI in a textview but you can use the url as you want. You can extract the values from it and perform some action.
Firstly you have to mention Intent Action_View of Browsable catregory in the android manifest with the particular activity you want to open on particular URL hit, it can be done as
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<activity android:name=".GetURL"> <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="http" android:host="mobikul.com" android:pathPrefix="/" /> <data android:scheme="http" android:host="www.webkul.com" android:pathPrefix="/" /> </intent-filter> </activity> |
Now your activity will be launched whenever a URL with the path as mentioned in <data> tag will be clicked. But remember the android system will still ask User whether he/she wants to open the URL with your app or with Browser.
Now lets get the URI in our Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class GetURL extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.geturl); TextView url_text = (TextView) findViewById(R.id.url); Intent in = getIntent(); String action = in.getAction(); if (Intent.ACTION_VIEW.equals(action)) { url_text.setText(in.getData().toString()); } } } |
And thats all. Try it yourself. Happy Coding!!