1 mobile software development framework: android 2/23/2011 y. richard yang

44
1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Upload: lilian-fowler

Post on 28-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

1

Mobile Software Development Framework:

Android

2/23/2011

Y. Richard Yang

Page 2: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

2

Admin.

Homework 2 due today Please schedule time to meet with the TA to

demo your program

Midterm: Wednesday after spring break

Page 3: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

3

Recap: Mobile Programming Requirements

Handle heterogeneous devices/configurations Be extremely efficiency on using resources

(memory, battery, …) Easy programming for event-driven

programming …

Page 4: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Recap: tinyOS

Customized OS for each app.

Reusability achieved through Components and interfaces structure

Execution model: two threads One for tasks One for event handling

4

Page 5: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

5

HelloWorldMIDlet.javaimport javax.microedition.midlet.*;import javax.microedition.lcdui.*;

public class HelloWorldMIDlet extends MIDletimplements CommandListener {

private Command exitCommand;private Display display;private TextBox t;

public HelloWorldMIDlet() { display = Display.getDisplay(this);exitCommand = new Command("Exit", Command.EXIT, 2);t = new TextBox(“CS434", "Hello World!", 256, 0);t.addCommand(exitCommand);t.setCommandListener(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);notifyDestroyed();

}}

}

Page 6: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

6

Recap: 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

Page 7: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Recap: Android

Scale down a popular programming environment to ease learning

Use virtual machines to mask device heterogeneity

Activity and View

Page 8: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Activity

A screen with which users can interact

Activity haslife cycle to yield resources

8

Page 9: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

View 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

Page 10: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

View by XML

Layout of visual interface

Java Code Initialize

Access TextView myTextView =

(TextView)findViewById(R.id.myTextView);

<?xml version=”1.0” encoding=”utf-8”?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”><TextView android:id=”@+id/myTextView” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Hello World, HelloWorld”/></LinearLayout>

@Overridepublic void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);}

main.xml

Page 11: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

User Interaction Event

onKeyDown. onKeyUp onTrackBallEvent onTouchEvent

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

registerButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) {….}}

Page 12: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Example: TipCal

12

Page 13: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Application Framework (Android): Key Concepts

Activity and view Visible screen for user interaction

External resources

13

Page 14: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

External Resources

14

Page 15: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Application Framework (Android): Key Concepts

Activity and view Visible screen for user interaction

External resources Service

15

Page 16: 1 Mobile Software Development Framework: Android 2/23/2011 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 2/23/2011 Y. Richard Yang

Application Framework (Android): Key Concepts

Activity and view Visible screen for user interaction

External resources Service Intercommunications

17

Communication among apps: - Intent- broadcast- data provider

Page 18: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Application 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.

Page 19: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent Description <Component name> Action

Data Category, e.g., LAUNCHER

19

Page 20: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent 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.sendOrderedBroadcast(), or Context.sendStickyBroadcast()) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code.

20

Page 21: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Android: Broadcast Receiver

Sending a broadcast: Context.sendBroadcast(Intent intent, String receiverPermission)

Context.sendOrderedBroadcast()

Receiving broadcast: Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter)

21

Page 22: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent Resolution: Explicit

Explicit intents: component identified

22

Make sure AndroidManifest.xml announces activities to be started

Intent myIntent = new Intent(IntentController.this, TipCal.class);startActivity(myIntent);

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

Page 23: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent Resolution: Implicit

Implicit intents System matches an intent object to the intent

filters of others

23

http://developer.android.com/guide/topics/intents/intents-filters.html

Page 24: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent filter

24

action

category

data

Page 25: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent Example II: Implicit

AndroidManifest.xml file for com.android.browser

25

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 26: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Intent Example II: Implicit

String action = "android.intent.action.DIAL";String phno = "tel:4326400";Uri data = Uri.parse(phno);Intent dialIntent = new Intent(action, data);startActivity(dialIntent);

Page 27: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

A Design Template: Invoker

27

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 28: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

A Design Template: Provider

28

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

For more complex data passing, please read the tutorial

Page 29: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

A Design Template: Provider

29

@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 30: 1 Mobile Software Development Framework: Android 2/23/2011 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);

30

Page 31: 1 Mobile Software Development Framework: Android 2/23/2011 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>

31

Page 32: 1 Mobile Software Development Framework: Android 2/23/2011 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

}

32

Page 33: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

See CS434 broadcast receiver examples

33

Page 34: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Service

Extends Service, providing onStartCommand()The system calls this method when another component,

such as an activity, requests that the service be started, by callingstartService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method.)onBind()The system calls this method when another component wants to bind with the service (such as to perform RPC), by callingbindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don't want to allow binding, then you should return null.onCreate()The system calls this method when the service is first created, to perform one-time setup procedures (before it calls eitheronStartCommand() or onBind()). If the service is already running, this method is not called.onDestroy()The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives.

34

Page 35: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Rules

Notify users

Background processing

35

Page 36: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Problem: Booking May Take a Long Time

36

ANRs (Application not responding) happen when Main thread (“event”/UI) does

not respond to input in 5 sec A broadcast receiver does not

finish in 10 sec 5-10 sec is absolute upper

bound

Page 37: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

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

37

Page 38: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Rules

Notify users

Background processing

38

Page 39: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Background Processing

Problem:

39

Page 40: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

public class MyActivity extends Activity {

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

    // Create runnable for posting    final Runnable mUpdateResults = new Runnable() {        public void run() {            updateResultsInUi();        }    };

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

        [ . . . ]    }

40

Page 41: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

public class MyActivity extends Activity {    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(mUpdateResults);            }        };        t.start();    }

    private void updateResultsInUi() {

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

41

Page 42: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Tools: AsyncTask

42

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!

Page 43: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Tools: android.app.IntentService An abstract Service

serializes the handling of the Intents passed upon service start

to use this class, extend it and implement onHandleIntent(Intent).

the Service will automatically be stopped when the last enqueued Intent is handled

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.

43

Page 44: 1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang

Example: Calendar's use of IntentService

public class DismissAllAlarmsService extends IntentService {

@Override public void onHandleIntent(Intent unusedIntent) {

ContentResolver resolver = getContentResolver();

...

resolver.update(uri, values, selection, null);

}

}

in AlertReceiver extends BroadcastReceiver, onReceive(): (main thread)

Intent intent = new Intent(context, DismissAllAlarmsService.class);

context.startService(intent);

44