Is it possible to make a link such as:
click me!
cause my Anton app to start up?
I know that this works for the Android Market app with the market protocol, but can something similar be done with other apps?
Here is an example of a link that will start up the Android Market:
click me!
Update:
The answer I accepted provided by eldarerathis works great, but I just want to mention that I had some trouble with the order of the subelements of the <intent-filter> tag. I suggest you simply make another <intent-filter> with the new subelements in that tag to avoid the problems I had. For instance my AndroidManifest.xml looks like this:
<activity android:name=".AntonWorld"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="anton" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Please DO NOT use your own custom scheme like that!!! URI schemes are a network global namespace. Do you own the "anton:" scheme world-wide? No? Then DON'T use it.
One option is to have a web site, and have an intent-filter for a particular URI on that web site. For example, this is what Market does to intercept URIs on its web site:
<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="market.android.com"
android:path="/search" />
</intent-filter>
Alternatively, there is the "intent:" scheme. This allows you to describe nearly any Intent as a URI, which the browser will try to launch when clicked. To build such a scheme, the best way is to just write the code to construct the Intent you want launched, and then print the result of intent.toUri(Intent.URI_INTENT_SCHEME).
You can use an action with this intent for to find any activity supporting that action. The browser will automatically add the BROWSABLE category to the intent before launching it, for security reasons; it also will strip any explicit component you have supplied for the same reason.
The best way to use this, if you want to ensure it launches only your app, is with your own scoped action and using Intent.setPackage() to say the Intent will only match your app package.
Trade-offs between the two:
http URIs require you have a domain you own. The user will always get the option to show the URI in the browser. It has very nice fall-back properties where if your app is not installed, they will simply land on your web site.
intent URIs require that your app already be installed and only on Android phones. The allow nearly any intent (but always have the BROWSABLE category included and not supporting explicit components). They allow you to direct the launch to only your app without the user having the option of instead going to the browser or any other app.
I think you'll want to look at the <intent-filter> element of your Manifest file. Specifically, take a look at the documentation for the <data> sub-element.
Basically, what you'll need to do is define your own scheme. Something along the lines of:
<intent-filter>
<data android:scheme="anton" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <--Not positive if this one is needed
...
</intent-filter>
Then you should be able to launch your app with links that begin with the anton: URI scheme.
I have a jQuery plugin to launch native apps from web links: https://github.com/eusonlito/jquery.applink
You can use it easily:
<script>
$('a[data-applink]').applink();
</script>
My Facebook Profile
I also faced this issue and see many absurd pages. I've learned that to make your app browsable, change the order of the XML elements, this this:
<activity
android:name="com.example.MianActivityName"
android:label="#string/title_activity_launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="http" />
<!-- or you can use deep linking like -->
<data android:scheme="http" android:host="xyz.abc.com"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
This worked for me and might help you.
Here's my recipe:
Create a static HTML that redirects to your requested app URL, put that page on the web.
That way, the links you share are 'real' links as far as Android is concerned ( they will be 'clickable').
You 'share' a regular HTTP link, www.your.server.com/foo/bar.html
This URL returns a simple 8 line HTML that redirects to your app's URI (window.location = "blah://kuku") (note that 'blah' doesn't have to be HTTP or HTTPS any more).
Once you get this up and running, you can augment the HTML with all the fancy capabilities as suggested above.
This works with the built-in browser, Opera, and Firefox (haven't tested any other browser). Firefox asks 'This link needs to be opened with an application' (ok, cancel). Other browsers apparently don't worry about security that much, they just open the app, no questions asked.
This method doesn't call the disambiguation dialog asking you to open either your app or a browser.
If you register the following in your Manifest
<manifest package="com.myApp" .. >
<application ...>
<activity ...>
<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:host="gallery"
android:scheme="myApp" />
</intent-filter>
</activity>
..
and click this url from an email on your phone for example
<a href="intent://gallery?directLink=true#Intent;scheme=myApp;package=com.myApp;end">
Click me
</a>
then android will try to find an app with the package com.myApp that responds to your gallery intent and has a myApp scheme. In case it can't, it will take you to the store, looking for com.myApp, which should be your app.
Once you have the intent and custom url scheme for your app set up, this javascript code at the top of a receiving page has worked for me on both iOS and Android:
<script type="text/javascript">
// if iPod / iPhone, display install app prompt
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i) ||
navigator.userAgent.match(/android/i)) {
var store_loc = "itms://itunes.com/apps/raditaz";
var href = "/iphone/";
var is_android = false;
if (navigator.userAgent.match(/android/i)) {
store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";
href = "/android/";
is_android = true;
}
if (location.hash) {
var app_loc = "raditaz://" + location.hash.substring(2);
if (is_android) {
var w = null;
try {
w = window.open(app_loc, '_blank');
} catch (e) {
// no exception
}
if (w) { window.close(); }
else { window.location = store_loc; }
} else {
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 5000) {
window.location = store_loc;
} else { window.close(); }
},
25);
window.location = app_loc;
}
} else {
location.href = href;
}
}
</script>
This has only been tested on the Android browser. I am not sure about Firefox or Opera. The key is even though the Android browser will not throw a nice exception for you on window.open(custom_url, '_blank'), it will fail and return null which you can test later.
Update: using store_loc = "https://play.google.com/store/apps/details?id=com.raditaz"; to link to Google Play on Android.
You may want to consider a library to handle the deep link to your app:
https://github.com/airbnb/DeepLinkDispatch
You can add the intent filter on an annotated Activity like people suggested above. It will handle the routing and parsing of parameters for all of your deep links. For example, your MainActivity might have something like this:
#DeepLink("somePath/{useful_info_for_anton_app}")
public class MainActivity extends Activity {
...
}
It can also handle query parameters as well.
Try my simple trick:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("classRegister:")) {
Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
}
view.loadUrl(url);
return true;
}
and my html link:
Go to register.java
or you can make < a href="classRegister:true" > <- "true" value for class filename
however this script work for mailto link :)
if (url.startsWith("mailto:")) {
String[] blah_email = url.split(":");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
startActivity(emailIntent);
}
Just want to open the app through browser? You can achieve it using below code:
HTML:
Click here
Manifest:
<intent-filter>
<action android:name="packageName" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
This intent filter should be in Launcher Activity.
If you want to pass the data on click of browser link, just refer this link.
Related
I really can't find anything for this, maybe I'm using the wrong keywords. Hope you can help anyway.
I developed an application which has the task to navigate a website in a easy way from mobile, but now I want it to be recognised outside from other applications as default for certain links. For example:
An email arrives from this website on your phone, when you click on the link in it, which rediricts to a page on that website, I want that the OS asks me to choose which application I want to use, in this case Google Chrome or my application.
Only thing I was able to do was to make it open YouTube links with the YouTube application externally, not viceversa from outside
Thanks fot the help and the idea, this is how i made it work:
Manifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.mywebsite"
android:scheme="https"
/>
Mainactivity.java (inside OnCreate)
WebView myWebView = (WebView) findViewById(R.id.webView);
Intent intent = getIntent();
Uri data = intent.getData();
if (data!=null) {
myWebView.loadUrl(String.valueOf(data)); //Load given link from external app
} else {
myWebView.loadUrl("http://www.mywebsite"); //Load homescreen
}
I am using this piece of code to launch my app from a link.
<activity
android:name="com.example.myApp.myClass"
android:label="#string/app_name" >
<intent-filter>
<data
android:host="customHostName"
android:scheme="customScheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This is href link, i want to get the key in the end.
customScheme://customHost/49FYTJTF00
It is working fine on all previous versions of android, but is not working on Lollipop.
When I click the link it only shows the list of browsers to launch.
What should I do?
Edit:
After testing and testing, I found that if your scheme contains an uppercase character the browser won't be able to launch it. Your scheme should only contain lowercase characters!
Also note that bug 459156 of the chromium project still doesn't allow you to launch url's directly, you should reference users to a webpage containing this JavaScript code:
<!DOCTYPE html>
<html>
<body>
<script language="javascript">
window.location = 'customscheme://customHost/49FYTJTF00';
</script>
</body>
</html>
Users landing on this page will automatically be prompted with either an Activity chooser dialog or directly send to your Activity.
To try it, open the Android browser go to the url below and copy paste the above snippet in the editor:
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_intro
Gif showing browser + JavaScript opening the Activity
Original post
I tried out your custom URI and it works on Android 5.0
But you should be aware of the following two bugs/issues:
Bug 459156 of the Chromium project This basicly means launching custom schemes from the Android browser does not work unless the URL is applied using JavaScript. For a workaround see this StackOverflow post: OAuth and custom scheme result in a "ERR_UNKNOWN_URL_SCHEME" in Chrome
Bug 80971: URI with custom scheme are not clickable in SMS Text (Linkify?)
My approach
I created two separate Activities, one as intent receiver and the other as intent launcher. The launching activity has an EditText where the full URI can be entered and a button to launch the entered URI.
Main.java onClick (Launching activity)
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(input.getText().toString()));
try {
startActivity(intent);
} catch(ActivityNotFoundException e){
Toast.makeText(this, "Auww!! No Activity can open this URI!", Toast.LENGTH_SHORT).show();
}
}
manifest.xml (only the activities)
Note the <data android:pathPattern=".*"/> part. this part is important so anything after the host will be accepted as valid.
<activity
android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".UriActivity"
android:label="#string/app_name">
<!--
This intent-filter will open any URI with the following forms:
customscheme://customHost/49FYTJTF00
customscheme://customHost
https://www.google.com/something/something
http://www.google.com/
http://google.com/something
-->
<intent-filter>
<data android:scheme="https"/>
<data android:scheme="http"/>
<data android:host="google.com"/>
<data android:host="www.google.com"/>
<data android:scheme="customscheme"/>
<data android:host="customHost"/>
<data android:pathPattern=".*"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity>
UriActivity.java (Receiving activity)
public class UriActivity extends ActionBarActivity {
private TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uri);
tvText = (TextView) findViewById(R.id.text);
setTextFromIntent();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setTextFromIntent();
}
private void setTextFromIntent(){
StringBuilder text = new StringBuilder();
Uri data = getIntent().getData();
if(data != null){
text.append("Path:\n");
text.append(data.getPath());
text.append("\n\nScheme:\n");
text.append(data.getScheme());
text.append("\n\nHost:\n");
text.append(data.getHost());
text.append("\n\nPath segments:\n");
text.append(Arrays.toString(data.getPathSegments().toArray()));
} else {
text.append("Uri is null");
}
tvText.setText(text);
}
}
Screenshot:
Test project download:
I made a download for the project if you wan't to try it out yourself.
Please use pathprefix.
android:pathPrefix="/"
It might solve your problem.
Please follow android developer guide URL.
Instead of using a link to customScheme://customHost/49FYTJTF00, have you tried using a link like
<a href="intent://customHostName/49FYTJTF00#Intent;scheme=customScheme;end">
Chrome should be able to open that in your app just fine. At the same time, this might not work on all browsers.
Keep in mind if you want run the app from Google Chrome you should do that in another way .
You wrote:
<data
android:host="customHostName"
android:scheme="customScheme" />
So, use customScheme://customHostName/49FYTJTF00
instead of customScheme://customHost/49FYTJTF00
This topic has been covered before, but I can't find an answer specific to what I'm asking.
Where I am: I followed hackmod's first piece of advice here: Make a link in the Android browser start up my app? and got this to work with a link in the webpage.
However, I'm having trouble understanding the second option (intent uri's). here's what I've got:
<activity android:name="com.myapps.tests.Layout2"
android:label="Auth Complete"
>
<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="mydomain.com"
android:path="/launch" />
</intent-filter>
</activity>
Now, with that I can go to "mydomain.com/launch" and it launches my activity. this all works well, except that I get the chooser. what I want is for it to just launch my activity without giving options.
From the explanation in the post I referenced it looks like thats what intent uris are for,but I can't find a straightforward example. what should my link in my webpage look like in order to launch this with no chooser?
I've seen a couple of examples that look something like this:
<a href="intent:#Intent;action=com.myapp.android.MY_ACTION;end">
However, that doesn't seem to work when I try it.
My test device is a Galaxy Tab 2.
any help would be appreciated.
I was also trying to launch the app in the recomended way. The following code worked for me.
Code inside the <activity> block of YourActivity in AndroidManifest.xml :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="your.activity.namespace.CUSTOMACTION" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Code in the activities onCreate() method :
Intent intent = getIntent();
if(intent != null && intent.getAction() == "your.activity.namespace.CUSTOMACTION") {
extraValue = intent.getStringExtra("extraValueName");
Log.e("AwseomeApp", "Extra value Recieved from intent : " + extraValue);
}
For the code in HTML, write an intent for launching the specific Activity, with the action your.activity.namespace.CUSTOMACTION, and your application package name your.activity.namespace. Your can also put some extra in the intent. For example intent.putExtra("extraValueName", "WOW"). Then get the required URL by printing the value of intent.toUri(Intent.URI_INTENT_SCHEME) in the Log. It should look like :
intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end
So your HTML code should look like :
<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end">
Launch App
</a>
This is as per what #hackbod suggested in here and this page from developers.google.com.
Depending on your intent filter this link should work:
start my app
But you should note that the android system will ask the user if your app or any other browser should be started.
If you want to avoid this implement a custom protcol handler. So just your app will listen for that and the user won't get the intent chooser.
Try to add this data intent:
<data android:scheme="mycoolapp" android:host="launch" />
With the code above this link should work:
start my app
I needed a small change to abhishek89m'a answer to make this work.
<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.YourActivity;S.extraValueName=WOW;end">
Launch App
</a>
I removed ".activity" after the slash in component name.
And I want to add, that custom action is probably the best answer to this problem if you don't want the app chooser to show up.
p.s. I would add this as comment, but I'm a new user and I don't have enough reputation points.
<a href="your.app.scheme://other/parameters/here">
This link on your browser will launch the app with the specific schema
like that on your intent
<intent-filter>
<data android:scheme="your.app.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Is it possible to make a link such as:
click me!
cause my Anton app to start up?
I know that this works for the Android Market app with the market protocol, but can something similar be done with other apps?
Here is an example of a link that will start up the Android Market:
click me!
Update:
The answer I accepted provided by eldarerathis works great, but I just want to mention that I had some trouble with the order of the subelements of the <intent-filter> tag. I suggest you simply make another <intent-filter> with the new subelements in that tag to avoid the problems I had. For instance my AndroidManifest.xml looks like this:
<activity android:name=".AntonWorld"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="anton" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Please DO NOT use your own custom scheme like that!!! URI schemes are a network global namespace. Do you own the "anton:" scheme world-wide? No? Then DON'T use it.
One option is to have a web site, and have an intent-filter for a particular URI on that web site. For example, this is what Market does to intercept URIs on its web site:
<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="market.android.com"
android:path="/search" />
</intent-filter>
Alternatively, there is the "intent:" scheme. This allows you to describe nearly any Intent as a URI, which the browser will try to launch when clicked. To build such a scheme, the best way is to just write the code to construct the Intent you want launched, and then print the result of intent.toUri(Intent.URI_INTENT_SCHEME).
You can use an action with this intent for to find any activity supporting that action. The browser will automatically add the BROWSABLE category to the intent before launching it, for security reasons; it also will strip any explicit component you have supplied for the same reason.
The best way to use this, if you want to ensure it launches only your app, is with your own scoped action and using Intent.setPackage() to say the Intent will only match your app package.
Trade-offs between the two:
http URIs require you have a domain you own. The user will always get the option to show the URI in the browser. It has very nice fall-back properties where if your app is not installed, they will simply land on your web site.
intent URIs require that your app already be installed and only on Android phones. The allow nearly any intent (but always have the BROWSABLE category included and not supporting explicit components). They allow you to direct the launch to only your app without the user having the option of instead going to the browser or any other app.
I think you'll want to look at the <intent-filter> element of your Manifest file. Specifically, take a look at the documentation for the <data> sub-element.
Basically, what you'll need to do is define your own scheme. Something along the lines of:
<intent-filter>
<data android:scheme="anton" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" /> <--Not positive if this one is needed
...
</intent-filter>
Then you should be able to launch your app with links that begin with the anton: URI scheme.
I have a jQuery plugin to launch native apps from web links: https://github.com/eusonlito/jquery.applink
You can use it easily:
<script>
$('a[data-applink]').applink();
</script>
My Facebook Profile
I also faced this issue and see many absurd pages. I've learned that to make your app browsable, change the order of the XML elements, this this:
<activity
android:name="com.example.MianActivityName"
android:label="#string/title_activity_launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data android:scheme="http" />
<!-- or you can use deep linking like -->
<data android:scheme="http" android:host="xyz.abc.com"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
This worked for me and might help you.
Here's my recipe:
Create a static HTML that redirects to your requested app URL, put that page on the web.
That way, the links you share are 'real' links as far as Android is concerned ( they will be 'clickable').
You 'share' a regular HTTP link, www.your.server.com/foo/bar.html
This URL returns a simple 8 line HTML that redirects to your app's URI (window.location = "blah://kuku") (note that 'blah' doesn't have to be HTTP or HTTPS any more).
Once you get this up and running, you can augment the HTML with all the fancy capabilities as suggested above.
This works with the built-in browser, Opera, and Firefox (haven't tested any other browser). Firefox asks 'This link needs to be opened with an application' (ok, cancel). Other browsers apparently don't worry about security that much, they just open the app, no questions asked.
This method doesn't call the disambiguation dialog asking you to open either your app or a browser.
If you register the following in your Manifest
<manifest package="com.myApp" .. >
<application ...>
<activity ...>
<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:host="gallery"
android:scheme="myApp" />
</intent-filter>
</activity>
..
and click this url from an email on your phone for example
<a href="intent://gallery?directLink=true#Intent;scheme=myApp;package=com.myApp;end">
Click me
</a>
then android will try to find an app with the package com.myApp that responds to your gallery intent and has a myApp scheme. In case it can't, it will take you to the store, looking for com.myApp, which should be your app.
Once you have the intent and custom url scheme for your app set up, this javascript code at the top of a receiving page has worked for me on both iOS and Android:
<script type="text/javascript">
// if iPod / iPhone, display install app prompt
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i) ||
navigator.userAgent.match(/android/i)) {
var store_loc = "itms://itunes.com/apps/raditaz";
var href = "/iphone/";
var is_android = false;
if (navigator.userAgent.match(/android/i)) {
store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";
href = "/android/";
is_android = true;
}
if (location.hash) {
var app_loc = "raditaz://" + location.hash.substring(2);
if (is_android) {
var w = null;
try {
w = window.open(app_loc, '_blank');
} catch (e) {
// no exception
}
if (w) { window.close(); }
else { window.location = store_loc; }
} else {
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 5000) {
window.location = store_loc;
} else { window.close(); }
},
25);
window.location = app_loc;
}
} else {
location.href = href;
}
}
</script>
This has only been tested on the Android browser. I am not sure about Firefox or Opera. The key is even though the Android browser will not throw a nice exception for you on window.open(custom_url, '_blank'), it will fail and return null which you can test later.
Update: using store_loc = "https://play.google.com/store/apps/details?id=com.raditaz"; to link to Google Play on Android.
You may want to consider a library to handle the deep link to your app:
https://github.com/airbnb/DeepLinkDispatch
You can add the intent filter on an annotated Activity like people suggested above. It will handle the routing and parsing of parameters for all of your deep links. For example, your MainActivity might have something like this:
#DeepLink("somePath/{useful_info_for_anton_app}")
public class MainActivity extends Activity {
...
}
It can also handle query parameters as well.
Try my simple trick:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("classRegister:")) {
Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
}
view.loadUrl(url);
return true;
}
and my html link:
Go to register.java
or you can make < a href="classRegister:true" > <- "true" value for class filename
however this script work for mailto link :)
if (url.startsWith("mailto:")) {
String[] blah_email = url.split(":");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
startActivity(emailIntent);
}
Just want to open the app through browser? You can achieve it using below code:
HTML:
Click here
Manifest:
<intent-filter>
<action android:name="packageName" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
This intent filter should be in Launcher Activity.
If you want to pass the data on click of browser link, just refer this link.
Can anybody please guide me regarding how to launch my android application from the android browser?
Use an <intent-filter> with a <data> element. For example, to handle all links to twitter.com, you'd put this inside your <activity> in your AndroidManifest.xml:
<intent-filter>
<data android:scheme="http" android:host="twitter.com"/>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application.
Of course, if you want to provide tight integration between your website and your app, you can define your own scheme:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
Then, in your web app you can put links like:
<a href="my.special.scheme://other/parameters/here">
And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme:// type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.
Edit: To answer your question, you can use getIntent().getData() which returns a Uri object. You can then use Uri.* methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:
Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"
You can do the above anywhere in your Activity, but you're probably going to want to do it in onCreate(). You can also use params.size() to get the number of path segments in the Uri. Look to javadoc or the android developer website for other Uri methods you can use to extract specific parts.
All above answers didn't work for me with CHROME as of 28 Jan 2014
my App launched properly from http://example.com/someresource/ links from apps like hangouts, gmail etc but not from within chrome browser.
to solve this, so that it launches properly from CHROME you have to set intent filter like this
<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:host="example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
<data
android:host="www.example.com"
android:pathPrefix="/someresource/"
android:scheme="http" />
</intent-filter>
note the pathPrefix element
your app will now appear inside activity picker whenever user requests http://example.com/someresource/ pattern from chrome browser by clicking a link from google search results or any other website
Please see my comment here: Make a link in the Android browser start up my app?
We strongly discourage people from using their own schemes, unless they are defining a new world-wide internet scheme.
In my case I had to set two categories for the <intent-filter> and then it worked:
<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
For example, You have next things:
A link to open your app: http://example.com
The package name of your app: com.example.mypackage
Then you need to do next:
Add an intent filter to your Activity
(Can be any activity you want. For more info check the documentation).
<activity
android:name=".MainActivity">
<intent-filter android:label="#string/filter_title_view_app_from_web">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://example.com" -->
<data
android:host="example.com"
android:scheme="http" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Create a HTML file to test the link or use this methods.
Open your Activity directly (just open your Activity, without a choosing dialog).
Open this link with browser or your programm (by choosing dialog).
Use Mobile Chrome to test
That's it.
And its not necessary to publish app in market to test deep linking =)
Also, for more information, check documentation and useful presentation.
There should also be <category android:name="android.intent.category.BROWSABLE"/> added to the intent filter to make the activity recognized properly from the link.
The following link gives information on launching the app (if installed) directly from browser. Otherwise it directly opens up the app in play store so that user can seamlessly download.
https://developer.chrome.com/multidevice/android/intents
Please note if your icon is disappear from android launcher when you implement this feature, than you have to split intent-filter.
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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-own-uri" />
</intent-filter>
</activity>
Yeah, Chrome searches instead of looking for scheme. If you want to launch your App through URI scheme, use this cool utility App on the Play store. It saved my day :)
https://play.google.com/store/apps/details?id=com.naosim.urlschemesender
Xamarin port of Felix's answer
In your MainActivity, add this (docs: Android.App.IntentFilterAttribute Class):
....
[IntentFilter(new[] {
Intent.ActionView },
Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
....
Xamarin will add following in the AndroidManifest.xml for you:
<activity
android:label="Something"
android:screenOrientation="portrait"
android:theme="#style/MyTheme"
android:name="blah.MainActivity">
<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="my.special.scheme" />
</intent-filter>
</activity>
And in order to get params (I tested in OnCreate of MainActivity):
var data = Intent.Data;
if (data != null)
{
var scheme = data.Scheme;
var host = data.Host;
var args = data.PathSegments;
if (args.Count > 0)
{
var first = args[0];
var second = args[1];
...
}
}
As far as I know, above can be added in any activity, not only MainActivity
Notes:
When user click on the link, Android OS relaunch your app (kill prev instance, if any, and run new one), means the OnCreate event of app's MainLauncher Activity will be fired again.
With this link: <a href="my.special.scheme://host/arg1/arg2">, in above last code snippet values will be:
scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2
Update: if android creates new instance of your app, you should add android:launchMode="singleTask" too.
Felix's approach to handling deep links is the typical approach to handling deep links. I would also suggest checking out this library to handle the routing and parsing of your deep links:
https://github.com/airbnb/DeepLinkDispatch
You can use annotations to register your Activity for a particular deep link URI, and it will extract out the parameters for you without having to do the usual rigmarole of getting the path segments, matching it, etc. You could simply annotate and activity like this:
#DeepLink("somePath/{someParameter1}/{someParameter2}")
public class MainActivity extends Activity {
...
}
Hey I got the solution. I did not set the category as "Default". Also I was using the Main activity for the intent Data. Now i am using a different activity for the intent data. Thanks for the help. :)
You need to add a pseudo-hostname to the CALLBACK_URL 'app://' doesn't make sense as a URL and cannot be parsed.
example.php:
<?php
if(!isset($_GET['app_link'])){ ?>
<iframe src="example.php?app_link=YourApp://blabla" style="display:none;" scrolling="no" frameborder="0"></iframe>
<iframe src="example.php?full_link=http://play.google.com/xyz" style="display:none;" scrolling="no" frameborder="0"></iframe>
<?php
}
else { ?>
<script type="text/javascript">
self.window.location = '<?php echo $_GET['app_link'];?>';
window.parent.location.href = '<?php echo $_GET['full_link'];?>';
</script>
<?php
}
Look #JRuns answer in here. The idea is to create html with your custom scheme and upload it somewhere. Then if you click on your custom link on your html-file, you will be redirected to your app. I used this article for android. But dont forget to set full name Name = "MyApp.Mobile.Droid.MainActivity" attribute to your target activity.
As of 15/06/2019
what I did is include all four possibilities to open url.
i.e, with http / https and 2 with www in prefix and 2 without www
and by using this my app launches automatically now without asking me to choose a browser and other option.
<intent-filter android:autoVerify="true">
<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:host="example.in" />
<data android:scheme="https" android:host="www.example.in" />
<data android:scheme="http" android:host="example.in" />
<data android:scheme="http" android:host="www.example.in" />
</intent-filter>