Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.

Similar presentations


Presentation on theme: "Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine."— Presentation transcript:

1 Networking: Part 1 (Web Content)

2 Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine used by Google Chrome. –Versions of Android prior to 4.4 (KitKat) were based on the WebKit open source web browser engine A view class, WebView, that can be used within an activity for displaying web content. Like the full-featured web browser, WebView also uses Chromium to render the web content. Standard Java features in package java.net for network access using TCP/IP sockets. Slide 2©SoftMoore Consulting

3 Launching an Internet Browser An Internet browser can be launched to display a web page by starting an activity with an intent whose action is ACTION_VIEW and whose data that is in the form of a URL. Uri uri = Uri.parse("http://www.google.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); If the URL is entered by the user as part of the application, then the following soft keyboard options should be used for the EditText field: –android:inputType="textUri" (provides ".com" and "/") –android:imeOptions="actionGo" (provides a "Go" button) Slide 3©SoftMoore Consulting

4 Class WebView Class WebView (in package android.webkit ) provides the ability to view web content within a portion of an activity’s screen. By default, some browser-related features such as JavaScript are disabled, but it is possible for an application to enable them when the view is created. In order to access the Internet, the following permission must be added to AndroidManifest.xml before the tag. Slide 4©SoftMoore Consulting

5 Defining a WebView in the Layout File (e.g., in activity_main.xml ) <WebView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin“ tools:context=".MainActivity" /> Slide 5©SoftMoore Consulting Note: A web view can be the root element for the xml file; i.e., it does not have to be nested within a layout element. If defined as the root element, it must include the namespace (“ xmlns ”) attribute.

6 Loading a URL private WebView webView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); // enable JavaScript webView.getSettings().setJavaScriptEnabled(true); // force links to open in WebView instead of browser webView.setWebViewClient(new WebViewClient()); webView.loadUrl("http://www.google.com"); } Slide 6©SoftMoore Consulting

7 Example: WebView Slide 7©SoftMoore Consulting

8 Loading HTML Defined Within the Application private static final String HELLO_HTML = " Hello, Android! ";... public void onCreate(Bundle savedInstanceState) {... webView.loadData(HELLO_HTML, "text/html", "utf-8"); } Slide 8©SoftMoore Consulting

9 Enhancing a WebView: Navigating Back to a Previous Page To allow navigation back to a previous pages, handle the “back” button on the device so that it will return to the previous page rather than exit the application. @Override public void onBackPressed() { if(webView.canGoBack()) webView.goBack(); else super.onBackPressed(); } Slide 9©SoftMoore Consulting Note: Older guides/tutorials might recommend using the onKeyDown() method and checking for KEYCODE_BACK, but the above is the preferred way to handle the “back” button.

10 Relevant Links Building Web Apps in WebView http://developer.android.com/guide/webapps/webview.html Getting Started: WebView-based Applications for Web Developers https://developer.chrome.com/multidevice/webview/gettingstarted Android WebView Tutorial http://www.androidaspect.com/2012/09/android-webview-tutorial.html Slide 10©SoftMoore Consulting


Download ppt "Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine."

Similar presentations


Ads by Google