Download presentation
Presentation is loading. Please wait.
Published byVivian Hood Modified over 9 years ago
1
Web APIs By Behrooz and Corey 1
2
10 mins... Punch It!! We will give a brief overview of the following topics: WebView WebSettings WebViewClient WebChromeClient Cross Platform Web Apps (if time permits) 2
3
Do you want a browser?? Sometimes you may want a full blown browser…if so, these aren’t the droids you’re looking for!! Just invoke a browser app with a URL Intent instead: Uri uri = Uri.parse("http://www.example.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); You can return to your app using the back button 3
4
WebView Basic view for rendering HTML Requires INTERNET permission in manifest file Content source: web, local storage, generated HTML Default WebView does not provide any widgets, JS or error handling. It must be customized to meet your specific need. But, if you do use it for JS or plugins…please handle onPause and onResume as good practice Each app has it's own cache (use clearCache) Always keep security in mind when modifying WebView settings and behavior 4
5
WebSettings Object encapsulating WebView state/settings Settings object acquired via WebView.getSettings tied to life of referenced WebView Of interest: setSupportZoom setAppCacheEnabled setJavaScriptEnabled setAllowFileAccessFromFileURLs setAllowUniversalAccessFromFileURLs (try to avoid using this) 5
6
WebViewClient Helper class for WebView rendering control and event handling (like form submissions, URL loading and errors) Defines and provides avenue for responding to all WebView errors (onReceivedError) Allows response to HTTP or SSL errors (onReceived*Error) 6
7
WebChromeClient Helper class for various browser UI events like progress updates, JS alerts etc... onShowFileChooser tells client to show file chooser to handle HTML forms with file input. Added in Lollipop(21). Previous versions of Android had undocumented hack for file uploads which was removed in Kitkat(19). Between WebView, WebViewClient and WebChromeClient you have a large array of tools for building web responsive apps 7
8
8 // Display the progress in the activity title bar getWindow().requestFeature(Window.FEATURE_PROGRESS); webview.getSettings().setJavaScriptEnabled(true); final Activity activity = this; webview.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int prog) { // Insert code to update activity progress bar } }); webview.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errCode, String descr, String faildUrl) { Toast.makeText(activity, "Oh no!" + descr, Toast.LENGTH_SHORT).show(); } }); webview.loadUrl("http://developer.android.com/");
9
Cross platform applications Using the Web APIs it is possible to set up a 2-way bridge between Java and JS Allowing JS to run Java native code and vise versa addJavaScriptInterface and onJsPrompt Allows for building hybrid web apps that are platform independent There are 3rd party frameworks for this like Apache Cordova 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.