Make a link in the Android browser start up my app?

前端 未结 9 1136
小鲜肉
小鲜肉 2020-11-21 22:06

Is it possible to make a link such as:

click me!

cause my Anton app to start

相关标签:
9条回答
  • 2020-11-21 22:58

    Try my simple trick:

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.startsWith("classRegister:")) {                  
                    Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
                }               
                view.loadUrl(url);
                return true;
            }
    

    and my html link:

    <a href="classRegister:true">Go to register.java</a>
    

    or you can make < a href="classRegister:true" > <- "true" value for class filename

    however this script work for mailto link :)

            if (url.startsWith("mailto:")) {
                String[] blah_email = url.split(":");
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("text/plain");
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
                Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
                startActivity(emailIntent);
            }
    
    0 讨论(0)
  • 2020-11-21 23:00

    Once you have the intent and custom url scheme for your app set up, this javascript code at the top of a receiving page has worked for me on both iOS and Android:

    <script type="text/javascript">
    // if iPod / iPhone, display install app prompt
    if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i) ||
        navigator.userAgent.match(/android/i)) {
      var store_loc = "itms://itunes.com/apps/raditaz";
      var href = "/iphone/";
      var is_android = false;
      if (navigator.userAgent.match(/android/i)) {
        store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";
        href = "/android/";
        is_android = true;
      }
      if (location.hash) {
        var app_loc = "raditaz://" + location.hash.substring(2);
        if (is_android) {
          var w = null;
          try {
            w = window.open(app_loc, '_blank');
          } catch (e) {
            // no exception
          }
          if (w) { window.close(); }
          else { window.location = store_loc; }
        } else {
          var loadDateTime = new Date();
          window.setTimeout(function() {
            var timeOutDateTime = new Date();
            if (timeOutDateTime - loadDateTime < 5000) {
              window.location = store_loc;
            } else { window.close(); }
          },
          25);
          window.location = app_loc;
        }
      } else {
        location.href = href;
      }
    }
    </script>
    

    This has only been tested on the Android browser. I am not sure about Firefox or Opera. The key is even though the Android browser will not throw a nice exception for you on window.open(custom_url, '_blank'), it will fail and return null which you can test later.

    Update: using store_loc = "https://play.google.com/store/apps/details?id=com.raditaz"; to link to Google Play on Android.

    0 讨论(0)
  • 2020-11-21 23:01

    Please DO NOT use your own custom scheme like that!!! URI schemes are a network global namespace. Do you own the "anton:" scheme world-wide? No? Then DON'T use it.

    One option is to have a web site, and have an intent-filter for a particular URI on that web site. For example, this is what Market does to intercept URIs on its web site:

            <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="http" android:host="market.android.com"
                    android:path="/search" />
            </intent-filter>
    

    Alternatively, there is the "intent:" scheme. This allows you to describe nearly any Intent as a URI, which the browser will try to launch when clicked. To build such a scheme, the best way is to just write the code to construct the Intent you want launched, and then print the result of intent.toUri(Intent.URI_INTENT_SCHEME).

    You can use an action with this intent for to find any activity supporting that action. The browser will automatically add the BROWSABLE category to the intent before launching it, for security reasons; it also will strip any explicit component you have supplied for the same reason.

    The best way to use this, if you want to ensure it launches only your app, is with your own scoped action and using Intent.setPackage() to say the Intent will only match your app package.

    Trade-offs between the two:

    • http URIs require you have a domain you own. The user will always get the option to show the URI in the browser. It has very nice fall-back properties where if your app is not installed, they will simply land on your web site.

    • intent URIs require that your app already be installed and only on Android phones. The allow nearly any intent (but always have the BROWSABLE category included and not supporting explicit components). They allow you to direct the launch to only your app without the user having the option of instead going to the browser or any other app.

    0 讨论(0)
提交回复
热议问题