Skip to content Skip to sidebar Skip to footer

Is There A Way To Pass A Message From An Android Browser To An App?

I have a situation where I'd like for some data to be passed from a mobile web site to a native Android app. A complication is that, in the normal case, the native Android app migh

Solution 1:

The solution is actually quite simple, I'm a bit astonished that nobody has been able to come up with it for over a year. So here it is:

The basic idea is to use cookies to store the information. The data flow is as follows:

open web page -> set cookie -> redirect to market -> install app -> launch browser with web page -> read cookie -> start custom intent with cookie data -> capture intent and read cookie data

The user visits a webpage (by scanning a QR Tag for example). The url of the webpage holds a reference to the data you want to pass, or holds the data itself. The website sets the data as a cookie and redirects to the Android market (http://market.android.com/details?id=com.yourapp). As soon as your app launches, you open a browser and load the same webpage again. This time however, it detects that the cookie is already set and redirects to a custom URL scheme like example://example.com/installed?id=DATA, instead of the market. Using an intent filter, you launch your activity and extract the information from the intent.

Here is the relevant Activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    if(intent == null || intent.getData() == null || !"example".equals(intent.getData().getScheme())) {
        String url = "http://example.com/yourApp/index.php";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
        finish();
        return;
    }else {
        Uri uri = intent.getData();
        if("example".equals(uri.getScheme())) {
            id = uri.getQueryParameter("id");
        }
    }
    ... initialise your activity ...
}

A simple PHP file (the website) for easy demonstration:

<?php
$value = "123";
if(!isset($_COOKIE["TestCookie"])) {
    setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */
    header("Location: http://market.android.com/details?id=com.yourapp");
    exit;
}else {
    header("Location: example://example.com/installed?id=".$_COOKIE["TestCookie"]);
    exit;
}
?>

And the intent filter:

<!-- Handle URLs like loyalty://example.com/merchant/123 -->
<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="example" android:host="example.com" />
</intent-filter>

BTW, I have decided to blog about the issue.


Solution 2:

Is there any other way to "leave a crumbtrail" or set a message that an app installed later can access from the Browser?

I think there is a simpler solution, a variation on the one employed by Barcode Scanner.

Step #1: Create a nice RESTful URL that contains the information you want. By RESTful I mean no ?, no #, none of that crap. It'll be a smidge simpler if this is on a distinct domain name, but this could just be something on your existing domain. For the purposes of this answer, I will assume that this URL looks like http://android.exampleapp.com/our/custom/data/goes/in/here/somewhere.

Step #2: For all URLs that could be created following the pattern of Step #1 (anything in http://android.exampleapp.com), your Web server should serve up a page saying "Hey! You don't have ExampleApp installed! If you click this link here, it will take you to the Android Market, where you can install ExampleApp! Then, try clicking the link that brought you to this page, and it will start up ExampleApp and give you...ummm...all this good data".

Step #3: Implement an activity in ExampleApp that has an <intent-filter> with the VIEW action, the BROWSEABLE category, and a <data> element identifying your scheme and domain (and, if needed, the base path, if that will be the same for all of these links).

What the user sees, if they do not have ExampleApp installed and click the link, is your Web page from step #2, from which they can go install ExampleApp.

What the user sees, if they do have ExampleApp installed and click the link, is your activity, which can grab the URL via getIntent().getData() and do something useful.


UPDATE

In a comment, you wrote:

mobile web page => store data => user installs app => user opens app => app retrieves data => app opens directly to the content retrieved

IMHO, you make too many assumptions about what the user is going to do. There may be days, weeks, or months of delay in between the arrows that I put in bold.

If you are going to distribute it yourself (e.g., off of your Web server), you always have the option of "burning" them a custom APK with the data inside of it, but then you have to deal with managing updates yourself.

Or, if you are going with an account-based system, have them create an account on your site before sending them to the Android Market. Store the data under that account, and when they connect back to that account from the app, you can match up the data with the app.


Solution 3:

Use a GET parameter: http://exampleapps.com?token=mK7Fyt5 Think of the way the YouTube app works. You're sent to the link for a video. If you don't have the YouTube app, you see the mobile site (which in your case could be an install link). If you do, you get asked whether to open the YouTube link with the native app, and the native app is passed the URL from which it was invoked, from which it can parse out the parameter for the video to show.

Use the following intent filter:

<intent-filter>
    <data android:scheme="http" android:host="exampleapps.com"/>
    <action android:name="android.intent.action.VIEW" />
</intent-filter>

Solution 4:

See: https://stackoverflow.com/a/7577379/710284

You can use an INSTALL_REFERRER link like:
http://market.android.com/details?id=your.package.name&referrer=your_wanted_parameter

After a user clicks this link and installs the application your broadcast receiver will receive a broadcast com.android.vending.INSTALL_REFERRER with "your_wanted_parameter" value.

More info:
http://code.google.com/mobile/analytics/docs/android/#android-market-tracking
Get referrer after installing app from Android Market
Get Android Google Analytics referrer tag


Post a Comment for "Is There A Way To Pass A Message From An Android Browser To An App?"