Redirect to Android app from website?

When a user opens a website, I need one of the following two cases:

  • If mycoolapp is installed, it will redirect the user to the application using the custom mycoolapp URL scheme: //
  • If the application is not installed, remain on the current page without publishing or redirecting to an unknown page or pop-up with an error.

Case 1 easily you can use the following code:

window.location = "mycoolapp://"; 

This will work if the application is installed on the device. When the application is not installed, it redirects the user to a blank page that is unknown.

For case 2, I have a solution using iFrame that works fine on iOS and in my native Android browser. It does not work in Chrome for Android.

 var redirect = function (location) { var iframe = document.createElement('iframe'); iframe.setAttribute('src', location); iframe.setAttribute('width', '1px'); iframe.setAttribute('height', '1px'); iframe.setAttribute('position', 'absolute'); iframe.setAttribute('top', '0'); iframe.setAttribute('left', '0'); document.documentElement.appendChild(iframe); iframe.parentNode.removeChild(iframe); iframe = null; }; redirect('mycoolapp://'); 

When the application is installed, it works in the native Android browser, but there is no redirection in Chrome for Android. Nothing happens on the page.

How can I redirect an application to Chrome for Android without redirecting to a blank page when the application is not installed?

Edit: I know you can use intention

 window.location = "intent://#Intent;package=com.mycoolapp;scheme=mycoolapp;launchFlags=268435456;end;"; 

This is not what I want, because it launches the application page in google play if the application is not installed. Is there a way to prevent it from being redirected to a Google game?

+5
source share
2 answers

You do not need to run the application on a user protocol. It will work for any address, for example. in AndroidManifest.xml :

 <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.mycoolapp.com" /> <data android:scheme="http" /> <data android:pathPattern="/Android" /> </intent-filter> 

This means that you can direct the user using:

 window.location = "/Android"; 

If the application is installed, Android will offer "Open with." If not, the user will simply be sent to the / Android page in his browser.

+4
source
  Add this in manifest file,note the scheme. <intent-filter> <data android:scheme="startci" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> 

in html gives the code, the scheme is the same as in the manifest file.

 <a href="intent:#Intent;scheme=startci;package=gramener.star;end">click here</a> 

if you want to add a parameter to your application use this

 <a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;end">click here</a> 

if the application is not installed, so if you want to redirect to some other page, add the return URL. url must be encoded

 <a href="intent:#Intent;scheme=startci://open?url_param=hi santhosh;package=gramener.star;S.browser_fallback_url=http%3A%2F%2Fzxing.org;end">click here</a> 

to add a parameter below the code

  Uri data = this.getIntent().getData(); if (data != null && data.isHierarchical()) { if (data.getQueryParameter("url_param") != null) { String param = data.getQueryParameter("url_param"); Log.d("the param is",param); //do something here } } 
0
source

Source: https://habr.com/ru/post/1201719/


All Articles