otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> Android

Title : Ways to open Android native app from browser
Total Reply : 0
Total View : 2099


Harry
Harry
Total thread: 131
Total reply: 50

Post #166
Ways to open Android native app from browser
1). Deep linking
a. Using standard scheme (recommended by Google)
b. Using custom scheme (from without our own controlled website, eg: www.11street.my)
<activity
  android:name=".intro.InitActivity"
  android:label="@string/app_name"
  android:launchMode="singleTop"
  android:screenOrientation="portrait"
  android:theme="@android:style/Theme.Light.NoTitleBar" >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

  <!-- 20150720 : add DeepLinking's intent-filter here -->
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <!-- Accepts URIs that begin with "http://m.11street.my/productdetail” -->
    <data
      android:host="m.11street.my"
      android:pathPrefix="/productdetail"
      android:scheme="http" />

  </intent-filter>

  <!-- 20150817 : Harry add custom scheme BECAUSE Jeff want to open app directly WITHOUT popup dialog -->
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <!-- 20150813 : Harry adds another scheme (BUT this "custom" scheme is discouraged !!!) -->
    <data android:scheme="elevenstmy" />
  </intent-filter>
</activity>
b. Using Intent filter (from Chrome)
REFERENCE: https://developer.chrome.com/multidevice/android/intents
<a href="intent://loadurl/http://m.11street.my/productdetail/genuine-lenovo-f309-external-hdd-1tb-grey-f309-902320/#Intent;scheme=elevenstmy;action=android.intent.action.VIEW;category=android.intent.category.BROWSABLE;package=my.elevenstreet.app;S.browser_fallback_url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dmy.elevenstreet.app%26referrer%3Dutm_source%253D11street_web%2526utm_medium%253Dweb%2526utm_term%253Dorganic%2526utm_content%253Dbanner_link%2526utm_campaign%253Dinternal_app_install;end'">
open app with Chrome intent scheme
</a>
Will trigger:
// 20150805 : Harry re-implement Deep Linking !!
String action = mIntent.getAction();
if(action != null && action.trim().length() > 0) {
  if(action.equalsIgnoreCase("android.intent.action.VIEW")) {

    Uri data = mIntent.getData();

    if(data == null) {
      LogHelper.e(TAG, "get deep linking action (android.intent.action.VIEW), BUT Intent.getData() is NULL");
    } else {
      String dataUri = data.toString();
      LogHelper.d(TAG, "get deep linking action (android.intent.action.VIEW), dataUri: " + dataUri);

      String scheme = data.getScheme();

      LogHelper.d(TAG, "scheme: " + scheme + ", dataUri: " + dataUri);

      // 20150818 : Harry implement a new check for custom scheme, eg: "elevenstmy://url/http://...."
      // MAKE SURE AndroidManifest.xml using the same Intent-Filter to catch this custom scheme !!
      final String customSchemeMy11 = "elevenstmy"; // from SchemeActivity.java

      String gotoUrl = null;

      if(scheme != null) {
        if (scheme.startsWith("http")) {// || scheme.startsWith("https")) {
          LogHelper.d(TAG, "FOUND normal standard scheme");
          gotoUrl = dataUri;
        } else if(scheme.startsWith(customSchemeMy11)) {
          LogHelper.d(TAG, "FOUND custom scheme: " + customSchemeMy11);

          // what is the command of this scheme ?
          // "elevenstmy://loadurl/http://m.11street.my/productdetail/..."
          // "elevenstmy://opentab/2"
          // "elevenstmy://reset/clear-cache"
          // "elevenstmy://reset/shared-preference"
          // "elevenstmy://forceappupgrade/"

          String command = dataUri.substring((scheme + "://").length());
          int i = command.indexOf("/");
          if(i >= 0) {
            command = command.substring(0, i);
          }
          LogHelper.d(TAG, "command: " + command);

          final String commandLoadUrl = "loadurl"; // from SchemeActivity.java
          final String commandOpenTab = "opentab"; // 20150826 : Harry create a new command as another example for KK !!
          if(command != null) {
            if (command.equalsIgnoreCase(commandLoadUrl)) {
              gotoUrl = dataUri.substring((customSchemeMy11 + "://" + command + "/").length());
              LogHelper.d(TAG, "FOUND command "" + commandLoadUrl + "": " + gotoUrl);
            } else if(command.equalsIgnoreCase(commandOpenTab)) {
              int tab = -1; // default value is negative (== invalid / no use)
              String sTab = dataUri.substring((customSchemeMy11 + "://" + command + "/").length());
              try {
                tab = Integer.parseInt(sTab);
              } catch (Exception e) {

              }
              LogHelper.d(TAG, "tab number: " + tab);

              if(tab >= 0) {
                // open tab !!!
                LogHelper.d(TAG, "tab number: " + tab);
                setTabChange(tab);
              } else {
                LogHelper.e(TAG, "INVALID tab number: " + tab);
              }
            } else {
              LogHelper.e(TAG, "UNHANDLED COMMAND "" + command + "" ... do NOT know what to do .. IGNORE IT !!!");
            }
          } else {
            LogHelper.e(TAG, "COMMAND is null ... do NOT know what to do .. IGNORE IT !!!");
          }
        } else {
          LogHelper.e(TAG, "UNHANDLED SCHEME: " + scheme + " ... do NOT know what to do .. IGNORE IT !!!");
        }
      } else {
        LogHelper.e(TAG, "SCHEME is NULL, do NOT know what to do .. IGNORE IT !!!");
      }

      if(gotoUrl != null && gotoUrl.trim().length() > 0 && scheme != null && scheme.trim().length() > 0) {
        LogHelper.d(TAG, "gotoUrl: " + gotoUrl);

        // 20150818 : add GA to track Deep Link
        HGoogleAnalyticWrapperSingleton.getInstance().send("DEEP_LINK_ANDROID", scheme, gotoUrl);

        HBComponentManager.getInstance().loadUrl(this, gotoUrl);
      } else {
        LogHelper.e(TAG, "INVALID gotoUrl: " + gotoUrl);

        // NOTE: should NOT come to this point because we did NOT register this Intent-Filter in our manifest
        // so make sure AndroidManifest.xml has proper Intent-Filter !!!
      }
    }
  } else {
    LogHelper.e(TAG, "UNHANDLE (TODO) action is received: " + action);
  }
} else {
  LogHelper.d(TAG, "mIntent.getAction() == null");
}
Falling in love with the world

Write : 2015-08-26 13:17:44

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 695,747

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird