networking: part 1 (web content). networking with android android provides a full-featured web...
TRANSCRIPT
![Page 1: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/1.jpg)
Networking: Part 1(Web Content)
![Page 2: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/2.jpg)
©SoftMoore Consulting
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
![Page 3: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/3.jpg)
©SoftMoore Consulting
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
![Page 4: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/4.jpg)
©SoftMoore Consulting
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 <application> tag.<uses-permission
android:name="android.permission.INTERNET" />
Slide 4
![Page 5: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/5.jpg)
©SoftMoore Consulting
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
Note: A web view can be the root element for the xml file; i.e., itdoes not have to be nested within a layout element. If defined asthe root element, it must include the namespace (“xmlns”) attribute.
![Page 6: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/6.jpg)
©SoftMoore Consulting
Loading a URL
private WebView webView;
@Overridepublic 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
![Page 7: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/7.jpg)
©SoftMoore Consulting
Example: WebView
Slide 7
![Page 8: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/8.jpg)
©SoftMoore Consulting
Loading HTML Defined Within the Application
private static final String HELLO_HTML = "<html><body>Hello, Android!</body></html>";
...
public void onCreate(Bundle savedInstanceState) { ... webView.loadData(HELLO_HTML, "text/html", "utf-8"); }
Slide 8
![Page 9: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/9.jpg)
©SoftMoore Consulting
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.@Overridepublic void onBackPressed() { if(webView.canGoBack()) webView.goBack(); else super.onBackPressed(); }
Slide 9
Note: Older guides/tutorials might recommend using theonKeyDown() method and checking for KEYCODE_BACK, butthe above is the preferred way to handle the “back” button.
![Page 10: Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine](https://reader035.vdocument.in/reader035/viewer/2022072017/56649f005503460f94c15c51/html5/thumbnails/10.jpg)
©SoftMoore Consulting
Relevant Links
• Building Web Apps in WebViewhttp://developer.android.com/guide/webapps/webview.html
• Getting Started: WebView-based Applications for Web Developershttps://developer.chrome.com/multidevice/webview/gettingstarted
• Android WebView Tutorialhttp://www.androidaspect.com/2012/09/android-webview-tutorial.html
Slide 10