Title: Mobile Software Development Framework: Android
1Mobile Software Development Framework Android
2/23/2011 Y. Richard Yang
2Admin.
- Homework 2 due today
- Please schedule time to meet with the TA to demo
your program - Midterm Wednesday after spring break
3Recap Mobile Programming Requirements
- Handle heterogeneous devices/configurations
- Be extremely efficiency on using resources
(memory, battery, ) - Easy programming for event-driven programming
4Recap tinyOS
- Customized OS for each app.
- Reusability achieved through
- Components and interfaces structure
- Execution model two threads
- One for tasks
- One for event handling
5HelloWorldMIDlet.java
import javax.microedition.midlet. import
javax.microedition.lcdui. public class
HelloWorldMIDlet extends MIDlet implemen
ts CommandListener private Command
exitCommand private Display display private
TextBox t public HelloWorldMIDlet()
display Display.getDisplay(this) exitCom
mand new Command("Exit", Command.EXIT, 2) t
new TextBox(CS434", "Hello World!", 256,
0) t.addCommand(exitCommand) t.setCommandL
istener(this) public void startApp()
display.setCurrent(t) public void pauseApp()
public void destroyApp(boolean
unconditional) public void
commandAction(Command c, Displayable s) if
(c exitCommand) destroyApp(false) no
tifyDestroyed()
6Recap J2ME
- Scale down a popular programming environment to
ease learning - Use virtual machines to mask device heterogeneity
- Use configuration/profile to handle device
heterogeneity to avoid using lowest common
denominator - MIDLet and Displayable to support user-interface
driven applications - MIDLet manages app life cycle
- Displayable has commands and provides command
listener - Introduce persistent record store
7Recap Android
- Scale down a popular programming environment to
ease learning - Use virtual machines to mask device heterogeneity
- Activity and View
8Activity
- A screen with which users can interact
- Activity haslife cycle to yield resources
9View
- A view component is a building block for user
interface components. - Widget Toolbox
- TextView, EditText, Button, Form, TimePicker
- ListView
- Layout
- Positions of controls
- LinearLayout, Relativelayout
http//developer.android.com/guide/tutorials/views
/index.htm
10View by XML
main.xml
- Layout of visual interface
- Java Code
- Initialize
- Access
- TextView myTextView
(TextView)findViewById(R.id.myTextView)
lt?xml version1.0 encodingutf-8?gt ltLinearLayo
ut xmlnsandroidhttp//schemas.android.com/a
pk/res/android androidorientationvertical
androidlayout_widthfill_parent
androidlayout_heightfill_parentgt ltTextView
androidid_at_id/myTextView androidlayout_widt
hfill_parent androidlayout_heightwrap_cont
ent androidtextHello World,
HelloWorld /gt lt/LinearLayoutgt
_at_Override public void onCreate(Bundle icicle)
super.onCreate(icicle) setContentView(R.layout.
main)
11User Interaction Event
- onKeyDown. onKeyUp
- onTrackBallEvent
- onTouchEvent
registerButton.setOnClickListener(new
View.OnClickListener() public void
onClick(View arg0) .
myEditText.setOnKeyListener(new OnKeyListener()
public boolean onKey(View v, int keyCode,
KeyEvent event) if (event.getAction()
KeyEvent.ACTION_DOWN) if (keyCode
KeyEvent.KEYCODE_DPAD_CENTER)
return true return false )
12Example TipCal
13Application Framework (Android) Key Concepts
- Activity and view
- Visible screen for user interaction
- External resources
14External Resources
15Application Framework (Android) Key Concepts
- Activity and view
- Visible screen for user interaction
- External resources
- Service
16Service Working in Background
- A basic function of Android Service
- A facility for an application to tell the
system about something it wants to be doing in
the background (even when the user is not
directly interacting with the application). - The system to schedule work for the service, to
be run until the service or someone else
explicitly stop it. - NO GUI, higher priority than inactive Activities
- Note
- A Service is not a separate process. The Service
object itself does not imply it is running in its
own process unless otherwise specified, it runs
in the same process as the application it is part
of. - A Service is not a thread. It is not a means
itself to do work off of the main thread (to
avoid Application Not Responding errors).
17Application Framework (Android) Key Concepts
- Activity and view
- Visible screen for user interaction
- External resources
- Service
- Intercommunications
- Communication among apps
- Intent
- broadcast
- data provider
18Application and Component Glues
- Intent
- An intent is an abstract description of an
operation to be performed. - To invoke operations from your own or others
- Can pass data back and forth between app.
- Intent Filter
- Register Activities, Services, and Broadcast
Receivers as being capable of performing an
action on a particular kind of data.
19Intent Description
- ltComponent namegt
- Action
- Data
- Category, e.g., LAUNCHER
20Intent Usage
- Pass to Context.startActivity() or
Activity.startActivityForResult() to launch an
activity or get an existing activity to do
something new. - Pass to Context.startService() to initiate a
service or deliver new instructions to an ongoing
service. - Pass to Context.bindService() to establish a
connection between the calling component and a
target service. It can optionally initiate the
service if it's not already running. - Pass to any of the broadcast methods (such as
Context.sendBroadcast(), Context.sendOrderedBroadc
ast(), or Context.sendStickyBroadcast()) are
delivered to all interested broadcast receivers.
Many kinds of broadcasts originate in system
code.
21Android Broadcast Receiver
- Sending a broadcast
- Context.sendBroadcast(Intent intent, String
receiverPermission) - Context.sendOrderedBroadcast()
- Receiving broadcast
- Intent registerReceiver (BroadcastReceiver
receiver, IntentFilter filter)
22Intent Resolution Explicit
- Explicit intents component identified
Intent myIntent new Intent(IntentController.this
,
TipCal.class) startActivity(myIntent)
- Make sure AndroidManifest.xml announces
activities to be started
ltapplication androidicon"_at_drawable/icon"
androidlabel"_at_string/app_name"gt
ltactivity androidname".IntentController"
androidlabel"Intent1"gt
ltintent-filtergt ltaction
androidname"android.intent.action.MAIN" /gt
ltcategory androidname"android.intent.
category.LAUNCHER" /gt
lt/intent-filtergt lt/activitygt
ltactivity androidname".TipCal"gtlt/activitygt
lt/applicationgt
23Intent Resolution Implicit
- Implicit intents
- System matches an intent object to the intent
filters of others
http//developer.android.com/guide/topics/intents/
intents-filters.html
24Intent filter
action
category
data
25Intent Example II Implicit
String action "android.intent.action.VIEW" Uri
data Uri.parse("http//www.google.com") Intent
myIntent new Intent(action, data) startActivity
(myIntent)
- AndroidManifest.xml file for com.android.browser
ltintent-filtergt ltaction androidname"android.
intent.action.VIEW" /gt ltcategory
androidname"android.intent.category.DEFAULT"
/gt ltscheme androidname"http" /gt ltscheme
androidname"https" /gt ltscheme
androidname"file" /gtlt/intent-filtergt
26Intent Example II Implicit
String action "android.intent.action.DIAL" Stri
ng phno "tel4326400" Uri data
Uri.parse(phno) Intent dialIntent new
Intent(action, data) startActivity(dialIntent)
27A Design Template Invoker
String action com.hotelapp.ACTION_BOOK" String
hotel hotel//name/ selectedHotel Uri
data Uri.parse(hotel) Intent bookingIntent
new Intent(action, data) startActivityForResults(
bookingIntent, requestCode)
28A Design Template Provider
ltactivity androidname".Booking"
androidlabelBooking"gt ltintent-filtergt
ltaction androidnamecom.hotelapp.ACTION_BOOK"
/gt ltdata androidschemehotel"
androidhostname/gt lt/intent-filtergt lt/activi
tygt
- For more complex data passing, please read the
tutorial
29A Design Template Provider
_at_Override public void onCreate(Bundle
savedInstanceState) super.onCreate(savedInst
anceState) setContentView(R.layout.main)
Intent intent getIntent() // why am I
called String action intent.getAction()
Uri data intent.getdata() String
hotelName data.getPath() // do the
booking setResult(RESULT_OK) finish()