Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to Android App from a Website?

When a user opens a website I want one of the following two cases to be happen:

  1. If the app mycoolapp is installed redirect the user to the app using the custom url scheme mycoolapp://
  2. If the app is not installed stay on the current page without posting or redirecting to an unknown page or an error popup.

Case 1 is easy you can use the following code:

window.location = "mycoolapp://"; 

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

For case 2 I have a solution using an iFrame which works great on iOS and on the native Android browser. It does not work on 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 app is installed it is working on the native Android browser, but on Chrome for Android there is not redirect. Nothing happens on the page.

How can I make redirect to my app working on Chrome for Android without redirecting to a blank page when the app is not installed?

Edit: I know that you can use an Intent

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

This is not what I want because it launches the app page on google play if the app is not installed. Is there a way that it will not redirect to google play?

like image 353
confile Avatar asked Sep 03 '14 11:09

confile


People also ask

How do I redirect a URL to an Android app?

Once you build a link with same signature as mentioned in manifest The android system will add your app in chooser to open link in your app and you will get the data in "getIntent(). getData()" in respective Activity. If app is not installed the link will itself open in browser. Then handle it on browser .

Can I open Android app from URL?

First of all you need to add intent filters for incoming links. Specify the ACTION_VIEW intent action so that the intent filter can be reached from Google Search. Add one or more tags, each of which represents a URI format that resolves to the activity. At minimum, the tag must include the android:scheme attribute.


1 Answers

 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 give the code, the scheme is same as in the manifest file.

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

if you want to add parameter to your app use this

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

if the app is not installed so if you want to redirect to some other page add the fallback url like this. the 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 get the parameter add below 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
        }
    }
like image 180
santhosh rb Avatar answered Sep 22 '22 09:09

santhosh rb