Launching an Android app from the Android Webkit browser

Posted on May 6, 2011

6


With my latest app VisualSpreads I wanted to integrate a user registration process. I already have a registration form on my website so I thought I should reuse this in my app. The plan was as follows:

1.) The user clicks a register button in the app, this fires up the android browser and loads my registration page(customised for mobile)
2.) The user fills in the form and clicks submit
3.) If all the form details are good the user account is created.
4.) The browser then re-launches the VisualSpread app at the login screen with the Username and Password fields pre-poulated.

The first three step are pretty easy, to launch the browser I use a standard intent:

Intent i = new Intent(Intent.ACTION_VIEW,
    		       Uri.parse("https://www.atplatforms.com/Account/Register/true"));
    		startActivity(i);

On the server I’m using asp.net MVC 2 to output and process the form using the out-of-the box AccountController register method with a couple of customisations. The route is defined as so /Account/Register/true. The “true” at the end is to indicate this request come from the app. This allows me to return a different response than the standard  html or redirect responses. I won’t go into details about the Register method here as it is not relevant to this post. I’ll be writing about this in a later article.

Once the user has filled in the form correctly the account is created. In a normal situation I’d redirect the user to the homepage or to a return Url. But in this case I want to launch my VisualSpreads Android app. This is actually a lot easier than it sounds as follows:

In the manifest for my login activity I declare the following intent filter .


Here i’m declaring a custome intent scheme

android:scheme="com.atplatforms.visualspreads"

This allows me to call the activity using the scheme as follows

com.appdragons.visualspreads://host/1stParameter/2ndParameter.

So in my case:

string redirectURL = String.Format("com.atplatforms.visualspreads://https/{0}/{1}", model.UserName, model.Password);
return new RedirectResult(redirectURL);

This will launch my app and pass in the Username and Password parameters using the URL. I then grab these values in the onCreate method and populate the relevant EditText fields.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri data = getIntent().getData();
        if(data != null){
            //the scheme data is in the format - customscheme://host/param0/param1"
            String scheme = data.getScheme();
            String host = data.getHost();
            List params = data.getPathSegments();
            username = params.get(0);
            password = params.get(1);
        }

        setContentView(R.layout.activity_login);
        mUsername = (EditText) findViewById(R.id.username);
        mPassword = (EditText) findViewById(R.id.password);
        mUsername.setText(username);
        mPassword.setText(password);
	}
Advertisements
Posted in: Android