Use jQuery Mobile to implement the Mobile news browser (Chapter 3) (1)

Source: Internet
Author: User

In the first two articles in this tutorial, I will introduce how to design a Mobile news browser using the jQuery Mobile framework, which implements a WEB-based news browser, in the last article of this tutorial, we will explain how to migrate the news browser of the implemented web version to the Android mobile phone. Note that, this article hopes that readers will have some basic Android knowledge.

Migrate to the Android app

In androidapplications, index.html will be used as an interface. We will compile an androidactivityclass to integrate index.html into android applications.

Modify index.html

First, modify NEWS_URI in index.html:

 
 
  1. var NEWS_URI = 'http://rss.news.yahoo.com/rss/'; 

Because we have not used bridge.php here, and we have integrated index.html into the Android application, there is no cross-origin ajax access problem. Next we will make the following changes:

 
 
  1. var EMPTY = ''; 
  2. ... 
  3. function changeLocation(varURI){ 
  4.   showProgress(); 
  5.   $.get(EMPTY,function(data){ 
  6.     window.location = varURI; 
  7.   }); 

The changeLocation () method will be called in android. webkit. WebViewClient. This method is mainly used to jump from the news classification page to the news details page.

1. First, showProgress () is used to display the waiting status of the Progress icon;

2. jQuery's get () method is actually a specific jQuery ajax () method. In the get method, we pass in an empty string and a callback event handle, set window. location is the address of the page for viewing specific news content. After the news content detail page is loaded, the wait icon is replaced to display the details of the news content page;

3. Because the browser progress bar exists in the web version, the changeLocation method is not necessarily required, but on mobile devices, there is a waiting icon that can give users a good user experience.

Compile Activity Class

Next, write the NewsActivity class. The Code is as follows:

 
 
  1. package com.news; 
  2.  
  3. import android.app.Activity; 
  4. import android.webkit.WebView; 
  5. import android.os.Bundle; 
  6. ... 
  7. public class NewsActivity extends Activity { 
  8.     WebView mWebView; 
  9.  
  10.   public void onCreate(Bundle savedInstanceState) { 
  11.     super.onCreate(savedInstanceState); 
  12.     setContentView(R.layout.main); 
  13.  
  14.     mWebView = (WebView) findViewById(R.id.webview); 
  15.     mWebView.setWebViewClient(new NewsClient()); 
  16.     mWebView.getSettings().setJavaScriptEnabled(true); 
  17.     mWebView.getSettings().setDomStorageEnabled(true); 
  18.     mWebView.loadUrl("android_asset/www/index.html"); 
  19.   } 
  20.   ... 

1. First, in the onCreate method, call the setContentView method to load the R. layout. main layout file in the res/layout folder;

2. Next, we use findViewById (R. id. webview) Get a WebView control instance and design a custom WebViewClient instance as a parameter for the WebView control. The custom WebViewClient is named NewsClient. We will introduce it later. Then we set the WebView container to support Javascript and Dom;

3. Finally, use loadUrl to load the previously created android_asset/www/index.html. On the news details page, when the user clicks the back button, the news category page is returned, in Android, we can capture the onKeyDown event Code as follows:

 
 
  1. public class NewsActivity extends Activity { 
  2.     WebView mWebView; 
  3.   public boolean onKeyDown(int keyCode, KeyEvent event) { 
  4.     if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
  5.       mWebView.goBack(); 
  6.       return true; 
  7.     } 
  8.     return super.onKeyDown(keyCode, event); 
  9.   } 
  10.   ... 

Next, let's take a look at how to compile a custom WebViewClient. The Code is as follows:

 
 
  1. public class NewsActivity extends Activity {  
  2.     WebView mWebView;  
  3.   private class NewsClient extends WebViewClient {  
  4.   
  5.     public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  6.       view.loadUrl("javascript:changeLocation('" + url + "')");  
  7.       return true;  
  8.     }  
  9.   }  
  10. ...  
  11. }  

First, we inherit the WebViewClient Android built-in browser class and overwrite the shouldOverrideUrlLoading method. In this method, we call the previously written changeLocation method, if we do not define our own WebViewClient, the detailed news content page will be displayed in a separate new browser, we want the news content page to be displayed on the same browser page.


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.