最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can you autolink dates in android webview - Stack Overflow

programmeradmin1浏览0评论

I have this html:

<p>
    <a href="">sawr-o-pel-te</a> meaning 'lizard shield'
</p>
<p>
    April 7th, 2015
</p>
<p>
    4/7/2015
</p>
<p>
    April 7th
</p>
<p>
    Next Monday 6th<br>
</p>
<p>
    202 E South St<br>
    Orlando, FL 32801
</p>
<h3>Quick Facts</h3>
<ul>
    <li>One of the most well-understood nodosaurids<span></span></li>
    <li>The earliest known genus of nodosaurid<span></span></li>
    <li>Measured about 5 meters (16.5 ft) long<span></span></li>
    <li>The tail made up nearly half of its body length</li>
</ul>
<span></span>

And I want to know if its possible to automatically hyperlink the dates so when the user presses on them you can add them to the users(phone's) calendar? A good example of how this should work is Gmail. When there is a date or the word(tomorrow, this friday, etc..) it should be automatically linked so the date can be added to calendar.

Update:

Does any one know if there is a ex. javascript that I can add to the app that will do this job for me?

I have this html:

<p>
    <a href="http://en.wikipedia/wiki/Sauropelta">sawr-o-pel-te</a> meaning 'lizard shield'
</p>
<p>
    April 7th, 2015
</p>
<p>
    4/7/2015
</p>
<p>
    April 7th
</p>
<p>
    Next Monday 6th<br>
</p>
<p>
    202 E South St<br>
    Orlando, FL 32801
</p>
<h3>Quick Facts</h3>
<ul>
    <li>One of the most well-understood nodosaurids<span></span></li>
    <li>The earliest known genus of nodosaurid<span></span></li>
    <li>Measured about 5 meters (16.5 ft) long<span></span></li>
    <li>The tail made up nearly half of its body length</li>
</ul>
<span></span>

And I want to know if its possible to automatically hyperlink the dates so when the user presses on them you can add them to the users(phone's) calendar? A good example of how this should work is Gmail. When there is a date or the word(tomorrow, this friday, etc..) it should be automatically linked so the date can be added to calendar.

Update:

Does any one know if there is a ex. javascript that I can add to the app that will do this job for me?

Share edited Apr 23, 2015 at 17:09 Darko Petkovski asked Apr 17, 2015 at 4:33 Darko PetkovskiDarko Petkovski 3,91213 gold badges56 silver badges120 bronze badges 3
  • Is the html on your question inside an android app ? – Pedro Lobito Commented Apr 30, 2015 at 13:38
  • @PedroLobito thanks for your answer, but this html is ming from the server side and I cannot add the #addtocalendar tag. So do you have any other workaround on your mind? – Darko Petkovski Commented Apr 30, 2015 at 21:20
  • yes, you can download the html source from the website, convert every link with a regex, or similar tool, and load the transformed html to webView with loadData, i.e. webView.loadData(stringWithHtml, "text/html; charset=UTF-8", null); – Pedro Lobito Commented May 1, 2015 at 2:04
Add a ment  | 

2 Answers 2

Reset to default 5 +25

Yes, it is possible.

Use the WebViewClient method shouldOverrideUrlLoading to intercept any links containing a certain value. Let's say you create a link like this:

<a href="#addtocalendar"> April 7th, 2015 - Add to Calendar</a>

Now, we'll use the shouldOverrideUrlLoading to intercept the tap and add the event to calendar, i.e.:

    mWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView wView, String url)
        {
            if (url.contains("addtocalendar") ) {
               Calendar cal = Calendar.getInstance();              
               Intent intent = new Intent(Intent.ACTION_EDIT);
               intent.setType("vnd.android.cursor.item/event");
               intent.putExtra("allDay", false);
               intent.putExtra("rrule", "FREQ=YEARLY"); //optional recurring event
               intent.putExtra("beginTime", cal.getTimeInMillis());
               intent.putExtra("endTime", cal.getTimeInMillis()+3600000); // adds 1 hour
               intent.putExtra("title", "Event on April 7th, 2015");
               startActivity(intent);

               return true;
           }
      }
});

Add the following permissions to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

shouldOverrideUrlLoading

boolean     shouldOverrideUrlLoading(WebView view, String url)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.

http://developer.android./reference/android/webkit/WebViewClient.html


Android Calendar Provider

http://developer.android./guide/topics/providers/calendar-provider.html

You can use this link to directly jump to particular date.

Where, date = YYYYMMDD and mode = day/month according to your need.

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论