Android automatically identifies the URL in textview or editview and opens it in its own application.

Source: Internet
Author: User

Textview has its own property Android: autolink = "Web". setting this property allows android to automatically identify the URL and click it to open it. However, this is the default browser to jump, we hopeProgram. At this time, we need to use linkify.

Private Static final string schema = "com. Youku. javaske: // message_private_url"; Private Static final string param_uid = "uid ";

Private void extracturl2link (textview v) {pattern wikiwordmatcher = pattern. compile ("(HTTP \: \/) | (HTTPS \: \/) | (www \\.)) [a-zA-Z0-9 _ \.] +) "); string mentionsscheme = string. format ("% S /? % S = ", schema, param_uid); linkify. addlinks (v, wikiwordmatcher, mentionsscheme );}

AboveCodeIt is our processing of textview, where pattern is used to identify the URI. Here we will identify the URI starting with HTTP, https, and www.

Then we write another activity to respond to the click event of textview.

Register the activity in androidmanifest. xml. The Code is as follows:

<Activity Android: Name = "mentionsactivity">
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "android. Intent. Category. browsable"/>
<Data Android: Scheme = "com. Youku. mongoke" Android: host = "message_private_url"/>

Then process the uploaded URI in this activity. The Code is as follows:

Private Static final string schema = "com. youku. required KE: // message_private_url "; Private Static final string param_uid =" uid "; Private Static final URI profile_uri = Uri. parse (schema );

Private void extractuidfromuri () {URI uri = getintent (). getdata (); If (Uri! = NULL & profile_uri.getscheme (). equals (URI. getscheme () {uid = Uri. getqueryparameter (param_uid);} If (UID. indexof ("www") = 0) {uid = "http: //" + uid;} else if (UID. indexof ("HTTPS") = 0) {string buid = uid. substring (5, uid. length (); uid = "HTTP" + buid ;}}

UID is the URI address we get. At this time, we can get it through webview based on the network address.

The following is a demo on csdn.


Related Article

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: 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.