1 mobile software development framework: android inter- thread, process communications 10/11/2012 y....

58
1 Mobile Software Development Framework: Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang

Upload: janice-charles

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

1

Mobile Software Development Framework:

Android Inter-Thread, Process Communications

10/11/2012

Y. Richard Yang

Page 2: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

2

Outline

Admin Android

Basic concepts• Activity, View, External Resources, Listener

Inter-thread communications• Handler, ASyncTask

Inter-process communications• Intent, Broadcast, BroadcastReceiver, Service

Page 3: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

3

Admin.

HW2 Due: Friday @ 11:55 pm

Page 4: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Recap: Mobile GUI App Workflow

App

App lifecycle callbacks/custom-start-pause-…

DisplayComposite

Display

DisplayComposite

Display

Display

DisplayComposite

Display

Display

Data/Model

Data/Model

Event Handler

Event Handler

DisplayComposite

DisplayDisplayComposite

Display Display

Page 5: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

5

Recap: Android UI App Basic Concepts

Activity

View/ViewGroup External definition of views in XML findViewById() to reduce coupling

Link view events to event handlers set…Listener()

Page 6: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: TipCalc

Set listener:

6

Page 7: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: TipCalc Event HandlerHandler:

7

Page 8: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Event Handler Execution

8

Event handler executed by the main/UI thread

UI events

systemevents

message

message

message

Looper UI (main) thread

http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/os/Looper.java.htm

Page 9: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Event Handler and Responsiveness

9

Event handler blocks events in the msg queue from being processed=>slow running handler leads to no UI response

http://developer.android.com/guide/practices/responsiveness.html

UI events

systemevents

message

message

message

Looper UI (main) thread

Page 10: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Responsiveness: Numbers (Nexus One) ~5-25 ms – uncached flash reading a byte ~5-200+(!) ms – uncached flash writing

tiny amount 100-200 ms – human perception of slow

action 108/350/500/800 ms – ping over 3G.

varies! ~1-6+ seconds – TCP setup + HTTP fetch

of 6k over 3G

10

Page 11: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Event Handler and ANR

11

Android system detects no response Main thread

(“event”/UI) does not respond to input in 5 sec

Page 12: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

12

Example

play_music

Page 13: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

13

Discussion

What are some design options if an event may take a while to be processed Time consuming loading process, e.g., slow

onCreate Heavy computation, e.g., voice recognition,

update map display Networking access …

Page 14: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

14

Typical Design Guidelines

Notify user E.g., progress bar, progress dialog A splash screen

If possible, non-blocking, incremental update UI E.g., gradual add items to map

Whenever possible, release UI thread ASAP Keep event handler simple Post heavy processing off the UI thread

Page 15: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: Background Thread

Use a background thread to do the task Background thread behavior controlled by

state State controlled by event handler

See PlayMusic

15

Page 16: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Service: 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).

Page 17: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Background Thread vs UI Thread

Problem: Background thread and UI thread are running

concurrently and may have race conditions if they modify UI simultaneously (e.g., UI switches to a different orientation)

A major sin of programming: concurrency bugs

Example: LoadingScreen

17

Page 18: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: LoadingScreen

18

public class LoadingScreen extends Activity implements Runnable {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading);

// start a new thread to load Thread thread = new Thread(this); thread.start(); }

public void run(){ longRunningTask(); setContentView(R.layout.main); }

…}

Page 19: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Solution

Background thread does not directly modify UI: send msg to UI thread, who processes the msg

19

Page 20: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Android Handler

Android’s mechanism to send and process Message and Runnable objects associated with a thread's MessageQueue.

Each Handler instance is associated with a single thread and that thread's message queue

A handler is bound to the thread / message queue of the thread that creates it

from that point on, it will deliver messages and runnables to that message queue

That thread processes msgs20

Page 21: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Android Handler

21

Page 22: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Using Handler: Examples

There are two main uses for a Handler

to schedule messages and runnables to be executed as some point in the future

• postDelayed(Runnable, delayMillis)

to enqueue an action to be performed on a different thread than your own.

• post(Runnable)

22

Page 23: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Handler

public class MyActivity extends Activity {

    [ . . . ]    // Need handler for callbacks to the UI thread    final Handler mHandler = new Handler();

    // Create runnable task to give to UI thread    final Runnable mUpdateResultsTask = new Runnable() {        public void run() {            updateResultsInUi();        }    };

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);

        [ . . . ]    }

23

Page 24: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Handler

    protected void startLongRunningOperation() {

        // Fire off a thread to do some work that we shouldn't do directly in the UI thread        Thread t = new Thread() {            public void run() {                mResults = doSomethingExpensive();                mHandler.post(mUpdateResultsTask);            }        };        t.start();    }

    private void updateResultsInUi() {

        // Back in the UI thread -- update our UI elements based on the data in mResults        [ . . . ]    }}

24

Page 25: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: Fixing LoadingScreen

25

Page 26: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: BackgroundTimer

26

Page 27: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Common Pattern

27

Page 28: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

AsyncTask as Abstraction

28

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { // on some background thread int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); } return totalSize; } protected void onProgressUpdate(Integer... progress) { // on UI thread! setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { // on UI thread! showDialog("Downloaded " + result + " bytes"); }}

new DownloadFilesTask().execute(url1, url2, url3); // call from UI thread!

See GoogleSearch

Page 29: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

29

Page 30: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

30

Outline

Admin Android

Basic concepts• Activity, View, External Resources, Listener

Inter-thread communications• Handler, ASyncTask

Inter-process communications• Intent, Broadcast, BroadcastReceiver, Service

Page 31: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

31

Inter-Process Communications (IPC)

Inter-thread communications are for one activity

Inter-process communication is designed to promote the development of complex applications, by allowing developers to reuse existing data and services from other applications.

One may also use IPC for intra-app communications (e.g., between Activities of the same app)

Page 32: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Discussion: IPC Use Cases

One component of Android sends messages to another component of Android

An IPC message in Android is called Intent

32

Component Component

Page 33: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Target: Activity

startActivity() or startActivityForResult() to launch an activity or get an existing activity to do something new.

33

Component Activity

Page 34: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Target: Service

startService() to initiate a service or deliver new instructions to an ongoing service.

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.

34

Component Service

Page 35: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Target: BroadcastReceiver

broadcastIntent()to send messages to all interested broadcast receivers. Many kinds of broadcasts originate in system code,

e.g., boot, battery low

35

Component BroadcastReceiver

Page 36: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Target: Data Provider

startActivityForResult()may target to a data provider (e.g., Contacts)

36

Component Data Provider

Page 37: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Example: A SocialApp

37

Page 38: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Android Application Component: Gang of Four

38

Page 39: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

39

Outline

Admin Android

Basic concepts• Activity, View, External Resources, Listener

Inter-thread communications• Handler, ASyncTask

Inter-process communications• Intent

Page 40: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Application and Component Glues: Intent

Intent An intent is an abstract description of an

operation to be performed.• Indicate operations from your own or others

http://developer.android.com/reference/android/content/Intent.html

Component Component

Page 41: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent Data Structure Primary pieces of info in an Intent

Action: The general action to be performed• ACTION_VIEW, ACTION_DIAL, ACTION_EDIT, …• Your own definition of strings

Data: a URI• tel:123• content://contacts/people/1• http://zoo.cs.yale.edu/classes/cs434• hotel://name/Omni_New_Haven

Other attributes Category Type (MIME type) Component (class name) Extras (key-value store)

41

scheme

host path

Page 42: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent Resolution: Explicit Intent Explicit Intent: specifies the exact class to run

42

class name

Context

Page 43: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Explicit Intent and Manifest

43

Make sure AndroidManifest.xml announces activities to be started

<application android:icon="@drawable/icon” android:label="@string/app_name" > <activity android:name=".IntentController” android:label="IntentController" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<activity android:name=".TipCal android:label="TipCal" > </activity>

Shown in Launcher

Announce class

See IntentController

Page 44: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Explicit Intent

44

YelpMapApp

Name: MapActivity

To: MapActivity

Only the specified destination receives this message

Page 45: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent Resolution: Implicit Intent

Intent does not specify exact class to run Info in the Intent used by the system to

determine the best component, at run time, to handle the intent

Matching based on Intent Filter Register Activities, Services, and Broadcast

Receivers register Intent Filter to indicate as being capable of performing an action on a particular kind of data

Q: benefit of dynamic binding?45http://developer.android.com/guide/topics/intents/intents-filters.html

Page 46: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Implicit Intents

46

Yelp

ClockApp

MapApp

Handles Action: VIEW

Handles Action: DISPLAYTIME

Implicit IntentAction: VIEW

Page 47: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Implicit Intents

47

Yelp

BrowserApp

MapApp

Handles Action: VIEW

Handles Action: VIEW

Implicit IntentAction: VIEW

Page 48: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent Filter

48

action

category

data

Page 49: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent Resolution: Implicit Intent

49

data

action

See IntentController

Page 50: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Implicit Intent and Manifest

AndroidManifest.xml file for com.android.browser

50

String action = "android.intent.action.VIEW";Uri data = Uri.parse("http://www.google.com");Intent myIntent = new Intent(action, data);startActivity(myIntent);

<intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <scheme android:name="http" />    <scheme android:name="https" />    <scheme android:name="file" /></intent-filter>

Page 51: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

A Design Template: Invoker

51

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);

Page 52: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

A Design Template: Provider

52

<activity android:name=".Booking" android:label=“Booking"> <intent-filter> <action android:name=“com.hotelapp.ACTION_BOOK" /> <data android:scheme=“hotel" android:host=“name”/> </intent-filter></activity>

Page 53: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

A Design Template: Provider

53

@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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();}

Page 54: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent and Broadcast: Sender

String action = "edu.yale.cs434.RUN";

Intent cs434BroadcastIntent = new Intent(action);

cs434BroadcastIntent.putExtra("message", "Wake up.");

sendBroadcast(cs434BroadcastIntent);

54

Example: IntentLaunch

Page 55: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent and Broadcast: Receiver

<receiver android:name=".CS434BroadcastReceiver" android:enabled="true">

<intent-filter>

<action android:name="edu.yale.cs434.RUN" />

</intent-filter>

</receiver>

55

Page 56: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent, Broadcast, Receiver, Notificationpublic class CS434BroadcastReceiver extends BroadcastReceiver {

public static final String CUSTOM_INTENT = "edu.yale.cs434.RUN";

// Display an alert that we've received a message.

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(CUSTOM_INTENT)) {

String message = (String)intent.getExtras().get("message");

CharSequence text = "Got intent " + CUSTOM_INTENT + " with " + message;

int duration = Toast.LENGTH_SHORT;

Toast mToast = Toast.makeText(context, text, duration);

mToast.show();

} // end of if

} // end of onReceive

}

56

Page 57: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Android: Content Provider

Each provider can expose its data as a simple table on a database model

Each content provider exposes a public URI that uniquely identifies its data set:

android.provider.Contacts.Phones.CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI android.provider.CallLog.Calls.CONTENT_URI android.provider.Calendar.CONTENT_URI

57

Page 58: 1 Mobile Software Development Framework: Android Inter- Thread, Process Communications 10/11/2012 Y. Richard Yang

Intent and Content Provider private void pickContact() {

    // Create an intent to "pick" a contact, as defined by the content provider URI    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);    startActivityForResult(intent, PICK_CONTACT_REQUEST);}

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST    if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {        // Perform a query to the contact's content provider for the contact's name        Cursor cursor = getContentResolver().query(data.getData(),        new String[] {Contacts.DISPLAY_NAME}, null, null, null);        if (cursor.moveToFirst()) { // True if the cursor is not empty            int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);            String name = cursor.getString(columnIndex);            // Do something with the selected contact's name...        }    }}

58