title: Launching an Android Application from the Browser
body: <p>I have looked at several Stack Overflow questions with similar titles to mine. Each is answered, and the original author seems satisfied, but when I try to replicate their results I come up empty handed. Obviously I'm doing something horribly wrong, but I can't seem to figure it out.</p>
  
  <p>For reference, these are the questions that I've been looking at:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2958701/launch-custom-android-application-from-android-browser">Launch Custom Android Application From Android Browser</a></li>
  <li><a href="https://stackoverflow.com/questions/3469908/make-a-link-in-the-android-browser-start-up-my-app">Make a Link in the Android Browser Start Up My App</a></li>
  <li><a href="https://stackoverflow.com/questions/5545803/android-dev-callback-url-not-working-0-o">Android Dev Callback Url Not Working</a></li>
  <li><a href="https://stackoverflow.com/questions/3586627/android-intent-filters-how-do-you-use-intent-category-browsable-correctly">Android Intent Filters: How Do You Use Intent Category Browsable Correctly</a></li>
  </ul>
  
  <p>Ultimately, I want to use this to restart my application once the user has performed an OAuth authentication request. However, my real application is a bit too large to post here. For this question, I've put together a simple application, to show you what I've done. I have two activities. This is mostly because every example I've seen uses an activity using the VIEW action. I'd prefer to use the MAIN action, but I've used both here to show that I tried both. </p>
  
  <p>AndroidManifest.xml (Original; see edited version below)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT"&gt;
  &lt;application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"&gt;
      &lt;activity android:name=".HelloAndroidActivity"&gt;
          &lt;intent-filter&gt;
              &lt;data android:scheme="ex1"/&gt;
              &lt;action android:name="android.intent.action.MAIN"/&gt;
              &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name=".CallbackActivity"&gt;
          &lt;intent-filter&gt;
              &lt;data android:scheme="ex2"/&gt;
              &lt;action android:name="android.intent.action.VIEW"/&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>AndroidManifest.xml (Edited in response to comment)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.example" android:versionCode="1" android:versionName="1.0-SNAPSHOT"&gt;
  &lt;application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"&gt;
      &lt;activity android:name=".HelloAndroidActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN"/&gt;
              &lt;category android:name="android.intent.category.LAUNCHER"/&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name=".CallbackActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.VIEW"/&gt;
              &lt;category android:name="android.intent.category.DEFAULT"/&gt;
              &lt;category android:name="android.intent.category.BROWSABLE"/&gt;
              &lt;data android:host="www.this-so-does-not-exist.com" android:scheme="http" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p>main.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  &gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>HelloAndroidActivity.java</p>
  
  <pre><code>package org.example;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class HelloAndroidActivity extends Activity {
  
      private TextView textView;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textView = (TextView) findViewById(R.id.textView);
      }
  
      @Override
      public void onResume() {
          super.onResume();
          Intent intent = getIntent();
          Uri data = intent.getData();
          if (data != null) {
              textView.setText("Hello Web!");
          }
      }
  
  }
  </code></pre>
  
  <p>CallbackActivity.java</p>
  
  <pre><code>package org.example;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class CallbackActivity extends Activity {
  
      private TextView textView;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textView = (TextView) findViewById(R.id.textView);
      }
  
      @Override
      public void onResume() {
          super.onResume();
          Intent intent = getIntent();
          Uri data = intent.getData();
          if (data != null) {
              textView.setText("Hello Web!");
          }
      }
  
  }
  </code></pre>
  
  <p>This builds happily enough. Then, inside the browser, if I enter ex1://helloworld or ex2://helloworld I get different results depending upon how it is done. If I enter it in the url bar I get a google search for ex1://helloworld. If I do it via a redirect, I get "Web page not available". If I try to start the intent directly with something similar to:</p>
  
  <pre><code>    Intent intent = new Intent(Intent.ACTION_MAIN);
      ComponentName componentName = new ComponentName(
              "org.example",
              "org.example.HelloAndroidActivity");
      intent.setComponent(componentName);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.setData(Uri.parse("ex1://helloworld"));
      intent.addCategory(Intent.CATEGORY_BROWSABLE);
      startActivity(intent);
  </code></pre>
  
  <p>I get the default "Hello my-example!". I'm not sure what I'm doing incorrectly. Any insights?</p>