Programming Mobile Applications for Android Handheld systems, Part 1

Course page: https://class.coursera.org/androidpart2-001
By Dr. Adam Porter

Source code available at: https://github.com/aporter/coursera-androidc

Week 5 - Threads, AsyncTasks and Handlers, and Networking

Threads, AsyncTasks and Handlers

Provide notes on this course.

Networking

  • Using HTTP get requests.
  • Processing HTTP responses, with JSON and XML.
  • Supoprt classes:
    • Socket (java.net)
      • using input/output streams
    • HttpRequest and HttpResponse (in org.apache)
    • Uri, AndroidHttpClient, AudioStream (in android.net)
    • HttpUrlConnection
      • using URL.openConnection()
    • AndroidHttpClient
      • create new instance with AndroidHttpClient.newInstance(“”)
      • create HttpGet(URL)
      • create ResponseHandler<String> (BasicResponseHandler for instance)
      • call client.execute(request, responseHandler);
  • JSON format
    • 2 types of structures:
      • maps
      • ordered lists
  • XML format
    • DOM (complete reading of the document object model)
    • SAX (stream reading with callbacks) parsers.
    • PULL similar to SAX but the reading of the next element is controlled by the application.

Week 6 - User Notifications, BroadcastReceiver and Alarms

User Notifications

Provide notes on this course.

BroadcastReceiver class

  • BroadcastReceiver class
    • Wait for events, receive them and react to them
    • Broadcast receiver register for the type of events they are interested in.
    • Intent are broadcasted
    • onReceive() method called with received intent.
  • Registration
    • Static registration:
      • <receiver> tag with <intent-filter> inside.
      • attributes:
        • android:enabled=[true|false]
        • android:exported=[true|false]
        • android:icon
        • android:name
        • android:permission
    • Dynamic registration
      • create IntentFilter object
      • create BroadcastReceiver
      • register BroadcastReceiver using registerReceiver()
        • from LocalBroadcastManager
        • context
      • call unRegisterReceiver()
  • Broadcast
    • Normal or ordered.
    • sticky or non-sticky.
    • With or without receiver permission.
    • debug details with: Intent.setFlag(FLAG_DEBUG_LOG_RESOLUTION)
    • adb dumpsys to get more info on broadcast receiver.
  • Processing
    • Event delivery:
      • onReceive():
        • context
        • intent
    • BroadcastReceiver are only valid while in onReceive() method.
    • sendOrderedBroadcast()
      • IntentFilter.setPriority()
    • abortBroadcast() ⇒ to stop a broadcast.
    • sendStickyBroadcast()
    • sendStickyOrderedBroadcast()
    • apps need BROADCAST_STICKY permission to broadcast these.
    • use isInitialStickyBroadcast() to check for cached sticky broadcast.

Alarms

  • Alarms
    • mechanism to send intent at some point(s) in the future.
    • Can wake up a device.
    • Alarms are cancelled in case of shutdown.
  • AlarmManager
    • getSystemService(ALARM_SERVICE)
    • set(type, triggerAtTime, PendingIntent);
    • setRepeating(type, triggerAtTime, interval, pendingIntent)
    • setInexactRepeating(…)
  • Alarm types
    • How to interpret the time:
      • Realtime : number of millisecs since 1 Jan 1970
      • system up time.
    • Want to do when device sleeps:
      • wake the device: RTC_WAKEUP / ELAPSED_REALTIME_WAKEUP
      • wait for wake up: RTC / ELAPSED_REALTIME
  • PendingIntent:
  • create with:
    • getActivity(…)
    • getBroadcast(…)
    • getService(…)

Week 7 - Graphics, Touch and Multimedia

Graphics and animation

  • 2D graphics
    • ImageView
      • Simple, less flexible
    • Canvas
      • Complicated, more powerfull.
      • drawText(), drawPoints(), drawColor(), drawOval(), drawBitmap(), etc.
      • GenericView for less frequent drawning.
      • SurfaceView for frequent drawing.
        • Drawing on a surface.
        • Subclass must implement SurfaceHolder.Callback
        • surfaceCreated()
        • surfaceChanged()
        • surfaceDestroyed()
        • to Draw:
          • SurfaceHolder.lockCanvas()
          • Canvas.drawBitmap(), etc
          • SurfaceHolder.unlockCanvasAndPost()
  • Drawable class:
    • ShapeDrawable
      • PathShape
      • RectShape
      • OvalShape
    • BitmapDrawable
    • ColorDrawable
  • View animation
    • size, position, transparency, etc…
    • TransitionDrawable
      • 2-layers drawable
    • AnimationDrawable
      • Show each frame for some time.
    • Animation
      • transformation of content of a view.
  • Property animation
    • ValueAnimator
      • Contains a TimeInterpolator
      • AnimatorUpdateListener
      • TypeEvaluator
      • AnimatorSet
      • ViewPropertyAnimator

Touch and Gestures

  • MotionEvents
    • Action code
      • ACTION_DOWN
      • ACTION_POINTER_DOWN
      • ACTION_POINTER_UP
      • ACTION_MOVE
      • ACTION_UP
      • ACTION_CANCEL
      • getActionMasked()
      • getActionIndex()
      • getPointerId(index)
      • getPointerCount()
      • getX(pindex)
      • getY(pindex)
      • findPointerIndex(pid)
    • time of event
    • device
    • location
    • pressure
    • MultiTouch:
      • Pointers with an unique ID
  • Touch Handling
    • View.onTouchEvent()
    • View.OnTouchListener
  • Gestures
    • GestureDetector
    • GestureDetector.OnGestureListener
    • GestureOverlayView
    • GestureLibraries.fromRawResource(…)

Multimedia

  • Audio, Images, Movies
    • AudioManager, SoundPool
      • getSystemService(Context.AUDIO_SERVICE)
      • Load and play sound effects
      • manage volume
      • SoundPool can mix streams
    • RingtoneManager, Ringtone
    • MediaPlayer
      • Playback of audio and video streams and files.
      • setDataSource()
      • prepare()
      • start()/pause()
      • seekTo()
      • stop()
      • release()
    • VideoView
    • MediaRecorder
      • setAudioSource()
      • setVideoSource()
      • setOutputFormat()
      • prepare()
      • start()
      • stop()
      • release()
  • Use camera
    • need uses-permission
    • need uses-feature for camera
    • get camera instance
    • set camera parameters
    • setup preview display
    • start preview
    • take a picture and process image data
    • release camera