Upcoming SlideShare
×

# Android Programming: Lecture 05

2,087

Published on

2) Intent Reception
6) Class homepage is at http://vkedco.blogspot.com/2011/08/android-programming-home.html

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
2,087
On Slideshare
0
From Embeds
0
Number of Embeds
29
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Android Programming: Lecture 05

1. 1. Android Programming Lecture 05 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
2. 2. Resources Source code and presentation materials for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-07.htmlwww.youtube.com/vkedco
5. 5. Intent Broadcast and Reception ● Let us implement a simple broadcast system ● There is one application that needs to compute the sum of the first n Fibonacci numbers ● There are two other applications that can compute the sum of the first n Fibonacci numberswww.youtube.com/vkedco www.vkedco.blogspot.com
6. 6. Intent Broadcast and Reception ● Assumption 1: The first application does not know of the existence of the other two applications so it cannot invoke them explicitly ● Assumption 2: The second and third applications do not know of the first onewww.youtube.com/vkedco www.vkedco.blogspot.com
7. 7. Intent Broadcast and Reception ● The first app broadcasts its request to compute the sum of the first n Fibonacci numbers – it is a broadcaster ● The second and third apps register themselves as broadcast receivers that respond to the specific type of request to compute the sum of the first n Fibonacci numberswww.youtube.com/vkedco www.vkedco.blogspot.com
8. 8. Intent Resolution ● What happens after the first application broadcasts its request through an intent? ● Android does intent resolution and invokes the second and/or third applications ● After the second or third broadcast receiver does its computation, it can create a result intent and broadcast it back to Androidwww.youtube.com/vkedco www.vkedco.blogspot.com
9. 9. Intent Broadcast and Reception ● The first app also registers itself with Android as a broadcast receiver to receive the events of that announce the computation of the n-th Fibonacci sum ● The Android invokes that receiver and it updates the user interfacewww.youtube.com/vkedco www.vkedco.blogspot.com
10. 10. Fibonacci Sum Systemwww.youtube.com/vkedco www.vkedco.blogspot.com
11. 11. Fibonacci Sum System ● FNSConsumer2 – broadcasts requests to compute the n-th Fibonacci sum and receives events that contain results of that computation ● FNSProviderIter2 – receives broadcasts to compute the n-th Fibonacci sum and broadcasts the result computed iteratively ● FNSProviderRec2 – receives broadcasts to compute the n-th Fibonacci sum and broadcasts the result computed recursivelywww.youtube.com/vkedco www.vkedco.blogspot.com
12. 12. Fibonacci Sum System ● FNSConsumer2 – in addition, this application can use three criteria: BEAUTY, EFFICIENCY, BOTH ● FNSProviderIter2 – responds to broadcasts for the n-th Fibonacci sum when EFFICIENCY or BOTH are specified ● FNSProviderRec2 – responds to broadcasts for the n- th Fibonacci sum when BEAUTY or BOTH are specifiedwww.youtube.com/vkedco www.vkedco.blogspot.com
13. 13. Fibonacci Sum System: FNSConsumer2 ● FNSConsumer2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, BEAUTY> } }www.youtube.com/vkedco www.vkedco.blogspot.com
14. 14. Fibonacci Sum System: FNSConsumer2 ● FNSConsumer2 – receives the following intent: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderIter2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
15. 15. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderIter2 – receives the following intent: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, CRITERION-VALUE> } }www.youtube.com/vkedco www.vkedco.blogspot.com
16. 16. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderIter2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderIter2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
17. 17. Fibonacci Sum System: FNSProviderRec2 ● FNSProviderRec2 – receives the following intents: ● Intent { action = “org.vkedco.android.intents.COMPUTE_FIB_SUM” extras = Bundle { <N, 5> <CRITERION, CRITERION-VALUE> } }www.youtube.com/vkedco www.vkedco.blogspot.com
18. 18. Fibonacci Sum System: FNSProviderIter2 ● FNSProviderRec2 – broadcasts the following intents: ● Intent { action = “org.vkedco.android.intents.FIB_SUM_COMPUTED” extras = Bundle { <FIB_SUM, 12> <SOURCE, FNSProviderRec2> } }www.youtube.com/vkedco www.vkedco.blogspot.com
19. 19. Nth Fibonacci Sum Projects Source Code FNSConsumer2, FNSProviderIter2, FNSProviderRec2www.youtube.com/vkedco www.vkedco.blogspot.com
22. 22. Grouping Menu Items ● Menu items can be grouped by assigning each item a group ID ● Multiple menu items that have the same group ID are considered part of the same group ● Android provides a set of methods that take group IDs and enable a specific functionality for all menu items in a selected groupwww.youtube.com/vkedco www.vkedco.blogspot.com
23. 23. Menu Item Attributes ● Menu items also have a title, an ID, and a sort-order ID (number) ● The sort-order ID is used to specify the order of menu items within a menu ● A menu item whose sort-order ID is 2 appears above a menu item whose sort-order ID is 4www.youtube.com/vkedco www.vkedco.blogspot.com
24. 24. Menu Creation ● Activity is always associated with a single Menu ● Such menus in Android are also called options menus ● Android creates this Menu automatically and passes it to the onCreateOptionsMenu callback method ● The method allows the programmer to populate the created Menu with MenuItemswww.youtube.com/vkedco www.vkedco.blogspot.com
28. 28. Menu Group Manipulation ● removeGroup(gid) – removes all menu items from that group ● setGroupCheckable(gid, checkable, exclusive) – show a check mark for all menu items in the group; if exclusive flag is true, only one menu item can be checked ● setGroupEnabled(gid, enabled) ● setGroupVisible(gid, visible)www.youtube.com/vkedco www.vkedco.blogspot.com
30. 30. Three Response Options ● onOptionsItemSelected() – override this method for the Activity class ● onMenuClickListener – create your own click listener and implement onMenuItemClick() ● Intent - use setIntent method of MenuItem; intents are called with setIntent(i)www.youtube.com/vkedco www.vkedco.blogspot.com
31. 31. Response 1: onOptionsItemSelected() @Override public boolean onCreateOptionsMenu(Menu mi) { switch ( mi.getItemId() ) { ... } return true; } ● For the menu items that are handled, this method should return truewww.youtube.com/vkedco www.vkedco.blogspot.com
32. 32. Response 1: onOptionsItemSelected() @Override public boolean onCreateOptionsMenu(Menu mi) { switch ( mi.getItemId() ) { ... } return super.onOptionsItemSelected(item); } ● For the menu items that are not handled, this method refer to superwww.youtube.com/vkedco www.vkedco.blogspot.com
34. 34. Response 3: Intent ● Either do not override onOptionsItemSelected() or override it and invoke the parent for the menu items that are not handled ● If you do not invoke the parent, the intents will not be handled properlywww.youtube.com/vkedco www.vkedco.blogspot.com
40. 40. Menu Harness: main.xml <?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/messageBoard" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>www.youtube.com/vkedco www.vkedco.blogspot.com