Programming for Mobile III

Android Studio 2.0

  • Preview version announced Monday, 11/23/2015
  • Adds several improvements including
    • Instant Run feature to reduce time between change in application and change on device/emulator
    • GPU Anaylsis
    • Interface emulate location or a series of locations
  • To see some examples in action, watch this talk

Sensor Listener Review

  • Event registration for sensors must be handled more responsibly than UI Events
  • SensorEventListeners caputre a wide range of sensors and accuracy changes

App Permissions

  • Apps running on Android (or any mobile system) are much more tightly controlled in what they can do
  • Accessing any resource outside of the app requires permission, including the internet or installing an icon on the phone
    • Many permissions must be specified but are granted by the OS without input from the user
    • Some permissions deal with more sensitive data and require the user agree for the app to access them, such as location or phone calls
  • The tag < uses-permision /> in the file AndroidManifest.xml is how permissions are set

Using Location

  • When building an app there are two different APIs that can be used for location
    • The android.location package
      • Provides low level access to location API
      • Doesn't require Google Play Services, is soley part of the Android system
    • The com.google.android.gms.location package
      • Higer level API
      • Requires Google approved device
      • Can be updated seperately from the OS

Play Services API Location

  • To use location from play services, first request permission for location
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    
  • Then a new client is created with specifications for the required API and the behavior on connection to that API

    mGoogleApiClient = new GoogleApiClient.Builder(this)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .addApi(LocationServices.API)
          .build();
    
  • After it is created, call the connect method (This isn't clear from the online docs!)

mGoogleApiClient.connect();

Play Services API Location Callbacks

  • In the prior example the event handler was this for both cases
    • The activity itself implemented both the ConnectionCallbacks and OnConnectionFailedListener interfaces
    • This interface specifies the method onConnected that runs when your application is connected to the API
    • Listening to this event will only get you the location one time.

Location Listener

  • To recieve repeated updates, a LocationListener is required
  • This is a bit more similar to listening to sensor events
    • A LocationRequest object defines the parameters of the requests, ie update frequency
    • The LocationListener handles the update through the onLocationChange event |

Localization

  • How do we make one application that can be deployed around the world in different languages
  • Thus far we have abstracted out the design from the code
    • We can further abstract out the text contents from the design?
  • Support for this is built in to Android apps

String Resources

  • To create a file of string constants, place them in the res/values directory in the file named strings.xml
  • The structure of this file looks like:
    <resources>
      <string name="app_name">CMSC331Ex</string>
      <string name="action_settings">Settings</string>
    </resources>
    
  • To access the string resource in XML use "@string/name"

Multiple String Resources

  • The string resources in the res/values directory are the default resources
  • To provide a string file based on locale, create strings.xml in the res/values-code directory
    • The code must be a valid 2 or 5 letter ISO code for a locale
      • fr for French
      • es for Spanish
      • de for German
      • es_rUS for American Spanish
  • The correct strings will automatically be used.