Android Programming
Yong Heui Cho @ Mokwon University
2
Context
• Interface to global information about an
application environment
• Uses of context
– Creating new objects
– Accessing standard common resources
– Accessing components implicitly
Activity
Context
OS
3
Access of Context
• this
– Activity extends Context
• View.getContext()
– Context for View (usually Activity Context)
• Activity.getApplicationContext()
– Permanent Application Context
• ContextWrapper.getBaseContext()
– Access to another Context
4
Bundle vs. Parcel
• Bundle
– A mapping from String values to various Parcelable
types
• Parcelable
– Interface for classes whose instances can be written
to and restored from a Parcel
• Parcel
– Container for a message (data and object
references) that can be sent through an IBinder
5
ContactsContract
• Contract between the contacts provider
and applications
• Context.getContentResolver()
• Cursor: interface of results for database
query
Content Provider
Contacts Provider
Activity
Context
OS
getContentResolver()
ContactsContract
6
Telephony Manager
• Access to the telephony APIs is managed by the
Telephony Manager.
– Monitor phone state
– Retrieve incoming phone numbers
– Observe changes to data connections, signal
strength, and network connectivity
7
PhoneStateListner
• Monitors changes in specific telephony
states on the device, including service state,
signal strength, message waiting indicator
(voicemail).
Activity
Context
PhoneStateListener
OS
TelephonyManager
listen()
8
Incoming Phone Calls
• Service
– an application component representing either an application's desire to
perform a longer-running operation while not interacting with the user or
to supply functionality for other applications to use
• IntentFilter
– Structured description of Intent values to be matched
– Context.registerReceiver(BR, IntentFilter)
– Context.unregisterReceiver(BR)
Activity
Context
OS
Service
broadcast
Intent
BroadcastReceiver
brTx
Context
registerReceiver() with IntentFilter
9
Receiving Call
• Permission
– <uses-permission
android:name="android.permission.READ_P
HONE_STATE" />
Activity
Context
OS
Service
Intent
PhoneStateListener
rxListener
Context
TelephonyManager
listen()
10
Sending SMS
• SmsManager
– SmsManager.getDefault().sendTextMessage(phoneNum, null,
msg, piSent, piDelivered);
• PendingIntent
– a description token for a foreign application to allow the foreign
application to use our application's permissions
– Action → Intent → PendingIntent(for broadcast)
Activity
Context
BroadcastReceiver
brSend
OS
SmsManager
broadcast
BroadcastReceiver
brDeliever
registerReceiver() with IntentFilter
PendingIntent
11
Receiving SMS
• Receiving process
– Broadcast → Intent → Bundle → PDU (Object[]) →
SmsMessage → String
• Bundle: a mapping (or dictionary) class from String
key to various types
• PDU: Protocol Description Unit
Activity
Context
BroadcastReceiver
brReceive
OS
SmsManager
Intent via broadcast
registerReceiver() with IntentFilter
12
Bluetooth Setup
• BluetoothAdapter (my device)
• BluetoothDevice (partner devices)
• void onActivityResult(int requestCode, int resultCode,
Intent data)
• Parcelable Intent::getParcelableExtra(String)
Activity
Context
BroadcastReceiver
brBluetooth
My OS
BluetoothAdapter
- getDefaultAdapter()
- isEnabled()
- startDiscovery()
- cancelDiscovery()
Intent via broadcast
registerReceiver() with IntentFilter
BluetoothDevice
- getName()
- getAddress()
Partner OS
Bluetooth

Android Programming

  • 1.
    Android Programming Yong HeuiCho @ Mokwon University
  • 2.
    2 Context • Interface toglobal information about an application environment • Uses of context – Creating new objects – Accessing standard common resources – Accessing components implicitly Activity Context OS
  • 3.
    3 Access of Context •this – Activity extends Context • View.getContext() – Context for View (usually Activity Context) • Activity.getApplicationContext() – Permanent Application Context • ContextWrapper.getBaseContext() – Access to another Context
  • 4.
    4 Bundle vs. Parcel •Bundle – A mapping from String values to various Parcelable types • Parcelable – Interface for classes whose instances can be written to and restored from a Parcel • Parcel – Container for a message (data and object references) that can be sent through an IBinder
  • 5.
    5 ContactsContract • Contract betweenthe contacts provider and applications • Context.getContentResolver() • Cursor: interface of results for database query Content Provider Contacts Provider Activity Context OS getContentResolver() ContactsContract
  • 6.
    6 Telephony Manager • Accessto the telephony APIs is managed by the Telephony Manager. – Monitor phone state – Retrieve incoming phone numbers – Observe changes to data connections, signal strength, and network connectivity
  • 7.
    7 PhoneStateListner • Monitors changesin specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail). Activity Context PhoneStateListener OS TelephonyManager listen()
  • 8.
    8 Incoming Phone Calls •Service – an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use • IntentFilter – Structured description of Intent values to be matched – Context.registerReceiver(BR, IntentFilter) – Context.unregisterReceiver(BR) Activity Context OS Service broadcast Intent BroadcastReceiver brTx Context registerReceiver() with IntentFilter
  • 9.
    9 Receiving Call • Permission –<uses-permission android:name="android.permission.READ_P HONE_STATE" /> Activity Context OS Service Intent PhoneStateListener rxListener Context TelephonyManager listen()
  • 10.
    10 Sending SMS • SmsManager –SmsManager.getDefault().sendTextMessage(phoneNum, null, msg, piSent, piDelivered); • PendingIntent – a description token for a foreign application to allow the foreign application to use our application's permissions – Action → Intent → PendingIntent(for broadcast) Activity Context BroadcastReceiver brSend OS SmsManager broadcast BroadcastReceiver brDeliever registerReceiver() with IntentFilter PendingIntent
  • 11.
    11 Receiving SMS • Receivingprocess – Broadcast → Intent → Bundle → PDU (Object[]) → SmsMessage → String • Bundle: a mapping (or dictionary) class from String key to various types • PDU: Protocol Description Unit Activity Context BroadcastReceiver brReceive OS SmsManager Intent via broadcast registerReceiver() with IntentFilter
  • 12.
    12 Bluetooth Setup • BluetoothAdapter(my device) • BluetoothDevice (partner devices) • void onActivityResult(int requestCode, int resultCode, Intent data) • Parcelable Intent::getParcelableExtra(String) Activity Context BroadcastReceiver brBluetooth My OS BluetoothAdapter - getDefaultAdapter() - isEnabled() - startDiscovery() - cancelDiscovery() Intent via broadcast registerReceiver() with IntentFilter BluetoothDevice - getName() - getAddress() Partner OS Bluetooth