JavaScript Web links Call Android Program

Source: Internet
Author: User

How to make Web links to launch Android applications, there are said to rewrite WebView related shouldoverrideurlloading method, but this theory can be achieved, because your Web page is not just by your own webview to browse, What you need to do is let the browser support on the market.

The main concern is about the conversion of intent between the string form and the intent object. The intent is passed as a string to the Java code in Android, and then parsed into the corresponding intent execution, mainly involving Java and JavaScript interaction.

Two forms of expression

Intent is a common API in Android development. Plays an essential role in the processing of Android components. Intent can exist in two ways.

    • The intent object. Used for processing in programs, when working with Android components.
    • A URI in the form of a string. Used in non-program code, such as in Web pages.

And here we solve the problem above us is the latter, the string form of intent.

Intent object to string URI

Intent provides two ways to convert an object to a string URI, one that is recommended, and a public String toUri (int flags) URI that converts the intent object into a string, in API 4. The URI in the form of a string can contain intent data,action,categories, type, flags, package, component, and extras.

Intent Intent = new Intent (); ComponentName comp = new ComponentName ("Com.mx.app.mxhaha", "com.mx.app.MxMainActivity"); intent.setcomponent (comp); LOG.I (LogTag, "intent.uri=" + Intent.touri (intent.uri_intent_scheme));

The resulting string URI is

Intent:#Intent; Component=com. MX. App. Mxhaha/com. MX. App. Mxmainactivity; End

  

Another method is that public String toURI () this is a deprecated method, because the string it generates begins with # and is placed on the link as an anchor point . Using this method is not recommended.

The intent object above uses Touri to convert to

#Intent; component=com.mx.app.mxhaha/com.mx.app.mxmainactivity;end

  

String URI to Intent object Getintent (string uri)

This method applies only to URIs that begin with # and are actually called within their methods parseUri(uri, 0) . This method has been compared to deprecated and is not recommended for use.

Getintentold (String URI)

Getintentold can support both the # start of the URI into the intent object, if the URI is not intent string form, then also return a intent, but its data part is a URI, Action is Android.intent.action.VIEW.

Parseuri (String URI, int flags)

This is the most complete method of conversion. Receive URI and flag as parameters. Supports transferring URIs as strings to intent objects.

The following is a method that resolves both the URI at the beginning of the intent: and the URI that can be parsed #Intent .

public static Intent parseintent (String url) {  Intent Intent = null;  Parse Intent URI into intent Object  int flags = 0;  Boolean Isintenturi = false;  if (Url.startswith ("intent:")) {      Isintenturi = true;      Flags = Intent.uri_intent_scheme;  } else if (Url.startswith ("#Intent;")) {      Isintenturi = true;  }  if (Isintenturi) {      try {          intent = Intent.parseuri (URL, flags);      } catch (URISyntaxException e) {          E.printstacktrace ();      }  }  return intent;}

  

For the application of the intent string form URI in the web link, there is a security risk, the vulnerability has already been found in the cloud platform, the use needs to be cautious. Using Addjavascriptinterface under Android 4.2 is a security breach, this article can be used http://www.pedant.cn/2014/07/04/ webview-js-java-interface-research/method to avoid this vulnerability, in addition to the parameter can be a custom object, Functuon asynchronous callback and so on, the return value can also be customized.

Transferred from: http://droidyue.com/blog/2014/11/23/start-android-application-when-click-a-link/

JavaScript Web links Call Android Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.