Giáo trình lập trình android

964 337 0
Giáo trình lập trình android

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 1 Android Development Introduction Victor Matos Cleveland State University Notes are based on:  Android Developers  A d id D l http://developer.android.com/index.html & Unlocking Android  by Frank Ableson, Charlie Collins, and Robi Sen.  ISBN 978‐1‐933988‐67‐2 Manning Publications, 2009 Chapter 1 ‐ Goals THE BIG PICTURE What is Android? Overview development environment Chapter 1 ‐ Chapter 1 ‐ Resources Android’s web page  http://www.android.com/ What is Android? • Android is an open‐source software platform  created by Google and the Open Handset  t db G l d th O H d t Alliance • It is primarily used to power mobile phones • It has the capability to make inroads in many  other (non‐phone) embedded application  markets Chapter 1 ‐ What is Android? • Android™ consists of a complete set of  software components for mobile devices  ft t f bil d i including: – an operating system,  – middleware, and  – embedded key mobile applications embedded key mobile applications – a large market.  Why Android? Listen from the project creators/developers  (2.19 min) – – – – – – – – – Nick Sears. Co‐founder of Android St Steve Horowitz. Engineering Director H it E i i Di t Dam Morrill. Developer Peisun Wu. Engineering Project Manager Erick Tseng. Project Manager Iliyan Malchev. Engineer Mike Cleron.  Software Manager Per Gustafsson. Graphics Designer etc… • http://www.youtube.com/watch?v=6rYozIZOgDk&eurl=http://www.android.com/about/&feature=player_embedd ed d • You will hear statements such as  “…currently it is too difficult to make new products … open software brings more  innovation … choices … lower costs … more applications such as family planner, my  taxes, understand my wife better, … ” Chapter 1 ‐ What is Open Handset Alliance? • Quoting from www.OpenHandsetAlliance.com page • “… Open Handset Alliance™, a group of 47 technology  and mobile companies  have come together to accelerate  innovation in mobile and offer consumers a richer, less  expensive, and better mobile experience.  • Together we have developed Android™, the first  complete open and free mobile platform complete, open, and free mobile platform • We are committed to commercially deploy handsets and  services using the Android Platform. “ Open Handset Alliance Members Operators Software Co Commercializat Semiconductor Handset  Manf China Mobile China Unicom KDDI Corp NTT DoCoMo Sprint Nextel T‐Mobile Telecom Italia Telefónica Vodafone Softbank … Ericsson Ascender Corp eBay Esmertec Google LivingImage NMS Comm Nuance Comm PacketVideo SkyPop SONiVOX … Borqs Aplix Noser Engineering Astonishing Tribe Wind River Systems Omron Software … Teleca Audience Broadcom Corp Intel Corp Marvell Tech Group Nvidia Corp Qualcomm SiRF Tech. Holdings Synaptics Texas Instr AKM Semicond AKM Semicond ARM Atheros Comm EMP ACER ASUS HTC LG Motorola Samsung ASUSTek Garmin Huawei Tech LG Samsung … Sony Ericsson Toshiba Chapter 1 ‐ See Android Developers http://www.youtube.com/watch?v=7Y4thikv‐OM Short video (4 min.)  Sh i D Showing Dave Bort B t and Dan Borstein,  two members of the  Android Open Source  Project talk about  the project The Android Platform Again, what did they say about Android? • Android is a software environment built for mobile  devices.  • It is not a hardware platform • Android includes:   • • • • • • • Linux kernel‐based OS,  a rich UI,  telephone functionality,  end‐user applications,  code libraries, code libraries,  application frameworks,  multimedia support,  • User applications are built for Android in Java.  10 Chapter 1 ‐ Android’s Context: Mobile Market Player$ Stakeholders: Mobile network operators want  to lock down their networks,  controlling and metering traffic Operators Device Software Manufacturers Vendors Device manufacturers want to  differentiate themselves with  features, reliability, and price  points Software vendors want complete  access to the hardware to deliver  cutting‐edge applications 11 The Maturing Mobile Experience Electronic tools of a typical business warrior Not so long ago … Today Phone Pager PDA Organizer Laptop Portable music player Portable music player No Internet access /  limited access Smartphone Laptop (perhaps!) Tomorrow ? 12 Chapter 1 ‐ The Maturing Mobile Experience I  want my 2015 Smartphone  to act as … Trying to answer Tomorrow ? Trying to answer: Tomorrow ? 10 10 11 12 13 14 15 16 17 18 Phone Pager PDA Organizer High Quality Camera (still & video) Portable music player Portable TV / Video Player / Radio Laptop Play Station GPS  G lf C dd (b ll Golf Caddy (ball retriever too) i ) Book Reader (I don’t read, It reads to me) Car / Home / Office Key Remote Control (Garage, TV, …) Credit Card / Driver’s License / Passport Cash on Demand Cook, house chores  Psychologist / Mentor / Adviser  ???? 13 Android vs. Competitors 1.Apple  Inc 2.Microsoft 3.Nokia 4.Palm Research In Motion 5.Research In Motion 6.Symbian 14 Chapter 1 ‐ The Size of  the Mobile  Market http://gizmodo.com/5489036 / ll h /cellphone‐overshare h [see appendix] 15 Android Components (Stack) • The Android stack includes a large array of  f t features for mobile applications f bil li ti • It would be easy to confuse Android with a  general purpose computing environment.  • All of the major components of a computing  platform are included platform are included 16 Chapter 1 ‐ Android Components • • • • • • • • • • Application framework enabling reuse and replacement of components  Dalvik virtual machine optimized for mobile devices  Integrated browser based on the open source WebKit engine  Optimized graphics powered by a custom 2D graphics library; 3D graphics  based on the OpenGL ES specification (hardware acceleration optional)  SQLite for structured data storage  Media support for common audio, video, and still image formats (MPEG4,  H.264, MP3, AAC, AMR, JPG, PNG, GIF)  GSM Telephony p y ((hardware dependent)  p ) Bluetooth, EDGE, 3G, 4G, and Wi‐Fi (hardware dependent)  Camera, GPS, compass, and accelerometer (hardware dependent)  Rich development environment including a device emulator, tools for  debugging, memory and performance profiling, and a plugin for the  Eclipse IDE  17 Android Components 18 Chapter 1 ‐ Android Components Video 1/3:  Android’s Architecture Presented by Mike Cleron, Google Corp. (13 min) Available at:  http://www.youtube.com/watch?v=QBGfUs9mQYY  19 Android Components Video 2/3:  Application’s Life Cycle Presented by Mike Cleron, Google Corp. (8 min) Available at:  http://www.youtube.com/watch?v=fL6gSd4ugSI&feature=channel 20 Chapter 1 ‐ 10 28 Designing Apps Using The WebView Control Victor Matos Cleveland State University Notes are based on: Android Developers http://developer.android.com/index.html The Busy Coder's Guide to Advanced Android Development by Mark L Murphy Ed CommonsWare 28 Android - WebView WebView An interesting class of Android applications could be created by combining JavaScripted HTML pages and the WebView control • The WebView widget is typically used by the WebKit browser engine to display pages accessed from the Internet • However, you could also display local HTML pages on a WebView The Android application interacts with the WebView through user created objects which are passed back and forth between the WebView and the Android Activities 28 Android - WebView WebView Architecture: HTML & Javascript pages + Android Activities Javascript uses custom object UI designed using HTML WebView 28 Android - WebView WebView What is New? The addJavascriptInterface() method on WebView allows you to pass a Java object from Android activities to the WebView, exposing its methods The various getters/setters methods defined in the object allow data exchange between the HTML-UI and Android activities In addition Javascript events (clicking buttons, making selections, filling boxes, etc) could be used to react to the user requests and correspondingly pass data from the UI to the Android classes 28 Android - WebView WebView Example: How is the HTML-Android exchange done? In this example the current coordinates (latitude, longitude) of the device will be displayed on the screen Assume: The application’s UI consists of a WebView called “browser”, also in the Assets folder the developer has introduced a Javascripted web-page called geoweb2.html The Android application has defined a custom object called “locater” with get/set methods exposing the *lat, lon] values Cont 28 Android - WebView WebView Example: How is the HTML-Android exchange done? (continuation) The following statements are held in the Android main activity browser.getSettings().setJavaScriptEnabled(true); browser.addJavascriptInterface(new Locater(), "locater"); browser.loadUrl("file:///android_asset/geoweb2.html"); The first allows the use of Javascript on the WebView The second statement passes the object type and name The last stat loads the local HTML page on the WebView Cont 28 Android - WebView WebView Example: How does the HTML page uses the object ? (continuation) The HTML page could manage the “locater” object through its accessors such as in the following lines: document.getElementById("lat").innerHTML=locater.getLatitude(); locater.setAddress ( document.getElementById(“address").innerHTML ); Locater.doSomething(); Where “lat” (and “address”) are HTML placeholders defined using (unknown) Cont 28 Android - WebView WebView Example: How does the HTML page uses the object ? (continuation) Consider the JavaScript expression: document.getElementById("lat").innerHTML • The innerHTML property is used along with getElementById within your JavaScript code to refer to an HTML element and change its contents • innerHTML allows you to change the page's content without refreshing the page (this makes your website feel quicker and more responsive to user input) • The innerHTML property is not actually part of the official DOM specification, despite this, it is supported in all major browsers including Android’s WebKit Cont 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView main.xml Add to the Manifest Cont 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView assets: geoweb2.html Android GeoWebOne Demo function whereami() { document.getElementById("lat").innerHTML=locater.getLatitude(); document.getElementById("lon").innerHTML=locater.getLongitude(); var spy = "Spy data coming from HTML\n" + document.getElementById("lat").innerHTML + "\n" + document.getElementById("lon").innerHTML; locater.setValue(spy); } function whereami2() { var spy = "Spy data coming from HTML\n" + document.getElementById("lat").innerHTML + "\n" + document.getElementById("lon").innerHTML; locater.htmlPassing2Android(spy); } You are at Latitude (unknown) Longitude (unknown) Click to Update Location Cont 10 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView package ucr.geowebtwo; // code based on M Murphy - CommonsWare, V Matos import import import import import import import import android.app.Activity; android.content.Context; android.os.Bundle; android.location.Location; android.location.LocationListener; android.location.LocationManager; android.webkit.WebView; android.widget.Toast; public class GeoWebTwo extends Activity { private private private Locater String PROVIDER = "gps"; WebView browser; LocationManager myLocationManager = null; locater = new Locater(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); browser = (WebView) findViewById(R.id.browser); // request GPS location services myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // enable JavaScript, pass user's object, load page browser.getSettings().setJavaScriptEnabled(true); browser.addJavascriptInterface(locater, "locater"); browser.loadUrl("file:///android_asset/geoweb2.html"); } Cont 11 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView @Override public void onResume() { super.onResume(); myLocationManager.requestLocationUpdates( PROVIDER, 3000, 10,onLocationChange); } @Override public void onPause() { super.onPause(); myLocationManager.removeUpdates(onLocationChange); } // -LocationListener onLocationChange = new LocationListener() { // passing the actual values of lat & lon Waiting for the function // whereami( ) to drop the arguments into HTML placeholders public void onLocationChanged(Location location) { StringBuilder buf = new StringBuilder("javascript:whereami("); buf.append(String.valueOf(location.getLatitude())); buf.append(","); buf.append(String.valueOf(location.getLongitude())); buf.append(")"); browser.loadUrl(buf.toString()); } public void onProviderDisabled(String provider) { // required for interface, not used } Cont 12 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView public void onProviderEnabled(String provider) { // required for interface, not used } public void onStatusChanged(String provider, int status, Bundle extras) { // required for interface, not used } }; Cont 13 28 Android - WebView WebView Complete Example: Get Location – Show on a Local WebView // /////////////////////////////////////////////////////////////// public class Locater { public String spy = ""; public double getLatitude() { Location loc = myLocationManager.getLastKnownLocation(PROVIDER); if (loc == null) { return (0); } return (loc.getLatitude()); } public double getLongitude() { Location loc = myLocationManager.getLastKnownLocation(PROVIDER); if (loc == null) { return (0); } return (loc.getLongitude()); } public void htmlPassing2Android(String dataFromHtml) { // changes to the HTML place-holders lat & lon can be seen here // There is an HTML button that when clicked calls this Android method spy = dataFromHtml; Toast.makeText(getApplicationContext(), spy, 1).show(); } } // Locater } // GeoWebTwo Last 14 28 Android - WebView WebView Questions Zipped code: 15 [...]...       ... android: label="@string/app_name"> 71 Android Manifest xml File...   51 Android Services Debugging ‐... field. It has specialized methods for reading each type of data.  69 Android Content Provider Example: Posting a query to the Contact list (1/2) package matos.cis493; import android. app.Activity; import android. net.Uri; import android. os.Bundle; import android. widget.EditText; import android. widget.Toast; import android. provider.Contacts.People; import android. content.ContentUris; import android. database.Cursor; public class AndDemo1... 58 Chapter 1 ‐ 29 Android Broadcast Receiver Broadcast Receiver Example (1/5). Intercept arriving SMS package matos.broadcastreceiver; matos b oadcast ecei e import import import import android. content.BroadcastReceiver; android. content.Context; android. content.Intent; android. content.IntentFilter; import android. util.Log; import po t a android. app.Activity; d o d app ct ty; import android. os.Bundle;... /> / i i 63 Android Content Provider • Content providers store and retrieve data and make it ... http://sites.google.com/site/io/inside‐the android application‐framework l / it /i /i id th d id li ti f k Android is designed to be fast, powerful, and easy to develop for. This session  will discuss the Android application framework in depth, showing you the  machinery behind the application framework explains the life‐cycle of an android apk. very good! 22 Chapter 1 ‐ 11 Android Components Video:  An Introduction to Android An Introduction to Android ...     ... android com/apk/res /android" package="matos.cis493" android: versionCode="1" android: versionName="1.0">

Ngày đăng: 03/04/2016, 17:22

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan