Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Android Programming Lecture 17: File and Network I/O Menus 11/9/2011 File and Network I/O Direct File I/O • Android API provides hook into the Android phone’s file system • These methods to open files for input and output – Methods don’t allow folder specification – Typically employ app‐specific permissions • Can employ with general Java I/O classes and methods Android & Java I/O: Text Output PrintWriter: use print() and println() method to write arbitrary strings of text, will automatically call toString() on objects BufferedWriter: handles buffering of data before writing to disk (writes in one big burst instead of each characters – important for efficiency) OutputStreamWriter: handles String to byte conversion FileOutputStream: writes bytes to a file on disk Android & Java I/O: Example Text Output Code Java I/O code requires try/catch blocks Try: Attempt to do file processing Catch: If something goes wrong, let me know Java I/O: Text Input Common wrapping (using Android openFileInput, which gives back a FileInputStream) Scanner scanner = new Scanner( new BufferedInputStream( openFileInput(filenameGoesHere))); Android & Java I/O: Text Input Scanner allows to read text by line, but also to parse out integers and doubles if they exist: hasNextLine() – are there more lines nextLine() – give me the next line hasNextDouble() – is the next thing a double nextDouble() – give me the next double …. BufferedInputStream supports buffering of reads from file (efficiency) FileInputStream supports reading of data from files Android & Java I/O: Text Input Continuation of program from a few slides back Accessing General Web Data • We have seen in previous slides how to access “web services” – Essentially, calling a function/method on another machine (details of how this works are hidden from programmer) • What if we just want to access HTML data? – Maybe we could grab Aramark menu data and have a PitMenu app? Accessing General Web Data • Direct HTTP access is needed commonly enough that the Android code API already supports it • One approach: URLConnection – Specify a URL – Request connection to that URL – Treat open connection as an input stream • We already know what to do at this point (from files) – Close connection • Because network based, requires try/catch to deal with potential errors URLConnection Specify a URL URL dataURL = new URL(“address_here”); Request connection to that URL URLConnection = dataURL.openConnection(); Treat open connection as an input stream BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()); Process data Above approach will allow line at a time reading via BufferedReader Close connection urlConnection.getInputStream().close(); URLConnection: Example Goal: Have app “scrape” web addresses out of an HTML web page – This technique is often used by “web‐crawlers” (such as Google) URLConnection: Example HTTPURLConnection One can cast a URLConnection made to a HTTP address as a HttpURLConnection and take advantage of a few HTTP specific methods One important example: int getResponseCode() , String getResponseMessage() Useful in handling protocol errors (404: page not found) URLConnection: Example URLConnection: OutputStream (POST) It is possible to use a URLConnection stream as an output stream as well Supports POST component of HTTP protocol, where data is sent to a server, such as when completing web forms urlConnection.getOutputStream(); Menus Menus • There is a hardware menu button on Android phones and tablets – Exposes Activity‐specific menus – These menus can hold up to six menu items, in a tiled fashion, or five items and a More button which displays an arbitrarily long list of additional menu options. • This is the “Expanded Menu”, and it is hooked to the bottom of the screen – Menus can trigger submenus, which are floating dialogs (they look much like drop down lists) Menus: RideTheWake Example Normal Menu • In the RideTheWake shuttle tracking app, the menu allows for: – Choosing a bus – Readjusting center if strayed away from bus – Checking for temporary schedule changes – Changing app settings (polling time) Expanded Menu Generating Menus: Meta Ideas • A menu consists of one or more MenuItems – Menu items in the primary (6‐option‐max) menu can be specified with an icon and text. – The ExpandedMenu and Submenus do not support icons, but do support Checkboxes and RadioButtons – All MenuItems can be associated with keyboard shortcuts and condensed titles Handling Selection Events: Meta Ideas • Activities are already designed to listen to menu button presses – Just have to write functions • Primary response mechanism: onOptionsItemSelected handler function – One function to handle any menu press • Can also associate with each menu item a function to support handling menu selection: – Use android:onClick=functionName attribute in XML Generating Menus: XML • Define an xml file in res/menu – Make one for each menu (activity) – May have to make this directory • Start with outer <menu> tag • Menu items are indicated by <item> – Associate with id, icon, title, checkable, radiobox, shortcuts (numeric and alphabetic) attributes – Submenus are defined as a menu within an item Full details for <menu> & <item> syntax is here: http://developer.android.com/guide/topics/resources/menu‐resource.html Generating Menus: XML Examples Simple three item menu: Bus menu: Three item menu with submenu: Icons are drawable resources Icons show up for first five, rest are on ExpandedMenu Triggering Menus When the menu hardware button is pressed, a listener function is called, which should inflate the menu XML: public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater= getMenuInflater(); inflater.inflate(R.menu.menu_name_here, menu); return true; } Alternatively, you could build the menu directly in Java here [calling MenuItem constructor, setting attributes], but we’ll stick with XML – see page 125 of book for Java approach Handling Selection Events For Menus A selected menu item is passed to the listener function: public boolean onOptionsItemSelected(MenuItem item) { // call super function of this function to support any upstream processing // check id, title, etc. to help decide how to act // return true if handled completely } Simple Menu Example