Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 38 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
38
Dung lượng
1,11 MB
Nội dung
282 CHAPTER 31 ■ INVOKING A SERVICE Here are the implementations of onResume() and onPause() for WeatherPlus: @Override public void onResume() { super.onResume(); registerReceiver(receiver, new IntentFilter(WeatherPlusService.BROADCAST_ACTION)); } @Override public void onPause() { super.onPause(); unregisterReceiver(receiver); } In onResume(), we register a static BroadcastReceiver to receive Intents matching the action declared by the service. In onPause(), we disable that BroadcastReceiver, since we will not be receiving any such Intents while paused, anyway. The BroadcastReceiver, in turn, simply arranges to update the forecast on the UI thread: private BroadcastReceiver receiver=new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { runOnUiThread(new Runnable() { public void run() { updateForecast(); } }); } }; And updateForecast() uses the interface stub to call into the service and retrieve the latest forecast page, also handling the case where the forecast is not yet ready (null): private void updateForecast() { try { String page=service. getForecastPage(); if (page==null) { browser.postDelayed(new Runnable() { public void run() { updateForecast(); } }, 4000); Toast .makeText(this, "No forecast available", 2500) .show(); } Murphy_2419-8C31.fm Page 282 Thursday, April 30, 2009 2:14 PM CHAPTER 31 ■ INVOKING A SERVICE 283 else { browser.loadDataWithBaseURL(null, page, "text/html", "UTF-8", null); } } catch (final Throwable t) { svcConn.onServiceDisconnected(null); runOnUiThread(new Runnable() { public void run() { goBlooey(t); } }); } } Murphy_2419-8C31.fm Page 283 Thursday, April 30, 2009 2:14 PM Murphy_2419-8C31.fm Page 284 Thursday, April 30, 2009 2:14 PM 285 ■ ■ ■ CHAPTER 32 Alerting Users via Notifications Pop-up messages. Tray icons and their associated “bubble” messages. Bouncing dock icons. You are no doubt used to programs trying to get your attention, sometimes for good reason. Your phone also probably chirps at you for more than just incoming calls: low battery, alarm clocks, appointment notifications, incoming text message or email, etc. Not surprisingly, Android has a whole framework for dealing with these sorts of things, collectively called notifications. Types of Pestering A service, running in the background, needs a way to let users know something of interest has occurred, such as when email has been received. Moreover, the service may need some way to steer the user to an activity where they can act upon the event—reading a received message, for example. For this, Android supplies status-bar icons, flashing lights, and other indicators collectively known as notifications. Your current phone may well have such icons, to indicate battery life, signal strength, whether Bluetooth is enabled, and the like. With Android, applications can add their own status-bar icons, with an eye towards having them appear only when needed (e.g., when a message has arrived). In Android, you can raise notifications via the NotificationManager. The NotificationManager is a system service. To use it, you need to get the service object via getSystemService (NOTIFICATION_SERVICE) from your activity. The NotificationManager gives you three methods: one to pester (notify()) and two to stop pestering (cancel() and cancelAll()). The notify() method takes a Notification, which is a data structure that spells out what form your pestering should take. The following sections describe all the public fields at your disposal (but bear in mind that not all devices will necessarily support all of these). Murphy_2419-8C32.fm Page 285 Tuesday, May 5, 2009 9:38 AM 286 CHAPTER 32 ■ ALERTING USERS VIA NOTIFICATIONS Hardware Notifications You can flash LEDs on the device by setting lights to true, also specifying the color (as an #ARGB value in ledARGB) and what pattern the light should blink in (by providing off/on durations in milliseconds for the light via ledOnMS and ledOffMS). You can play a sound, using a Uri to a piece of content held, perhaps, by a ContentManager (sound). Think of this as a ringtone for your application. You can vibrate the device, controlled via a long[] indicating the on/off patterns (in milliseconds) for the vibration (vibrate). You might do this by default, or you might make it an option the user can choose when circumstances require a more subtle notification than an actual ringtone. Icons While the flashing lights, sounds, and vibrations are aimed at getting somebody to look at the device, icons are designed to take them the next step and tell them what’s so important. To set up an icon for a Notification, you need to set two public fields: icon, where you provide the identifier of a Drawable resource representing the icon, and contentIntent, where you supply a PendingIntent to be raised when the icon is clicked. You should be sure the PendingIntent will be caught by something, perhaps your own application code, to take appro- priate steps to let the user deal with the event triggering the notification. You can also supply a text blurb to appear when the icon is put on the status bar (tickerText). If you want all three, the simpler approach is to call setLatestEventInfo(), which wraps all three of those in a single call. Seeing Pestering in Action Let us now take a peek at the Notifications/Notify1 sample project (available in the Source Code section at http://apress.com), in particular the NotifyDemo class: public class NotifyDemo extends Activity { private static final int NOTIFY_ME_ID=1337; private Timer timer=new Timer(); private int count=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.notify); Murphy_2419-8C32.fm Page 286 Tuesday, May 5, 2009 9:38 AM CHAPTER 32 ■ ALERTING USERS VIA NOTIFICATIONS 287 btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { TimerTask task=new TimerTask() { public void run() { notifyMe(); } }; timer.schedule(task, 5000); } }); btn=(Button)findViewById(R.id.cancel); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); mgr.cancel(NOTIFY_ME_ID); } }); } private void notifyMe() { final NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification note=new Notification(R.drawable.red_ball, "Status message!", System.currentTimeMillis()); PendingIntent i=PendingIntent.getActivity(this, 0, new Intent(this, NotifyMessage.class), 0); note.setLatestEventInfo(this, "Notification Title", "This is the notification message", i); note.number=++count; mgr.notify(NOTIFY_ME_ID, note); This activity sports two large buttons, one to kick off a notification after a five-second delay, and one to cancel that notification (if it is active). See Figure 32-1. Murphy_2419-8C32.fm Page 287 Tuesday, May 5, 2009 9:38 AM 288 CHAPTER 32 ■ ALERTING USERS VIA NOTIFICATIONS Figure 32-1. The NotifyDemo activity main view Creating the notification, in notifyMe(), is accomplished in five steps: 1. Get access to the NotificationManager instance. 2. Create a Notification object with our icon (red ball), a message to flash on the status bar as the notification is raised, and the time associated with this event. 3. Create a PendingIntent that will trigger the display of another activity (NotifyMessage). 4. Use setLatestEventInfo() to specify that, when the notification is clicked on, we are to display a certain title and message, and if that is clicked on, we launch the PendingIntent. 5. Tell the NotificationManager to display the notification. Hence, if we click the top button, after five seconds our red ball icon will appear in the status bar. Our status message will also appear briefly, as shown in Figure 32-2. If you click on the red ball, a drawer will appear beneath the status bar. Drag that drawer all the way to the bottom of the screen to show the outstanding notifications, including our own, as shown in Figure 32-3. Murphy_2419-8C32.fm Page 288 Tuesday, May 5, 2009 9:38 AM CHAPTER 32 ■ ALERTING USERS VIA NOTIFICATIONS 289 Figure 32-2. Our notification as it appears on the status bar, with our status message Figure 32-3. The notifications drawer, fully expanded, with our notification Murphy_2419-8C32.fm Page 289 Tuesday, May 5, 2009 9:38 AM 290 CHAPTER 32 ■ ALERTING USERS VIA NOTIFICATIONS If you click on the notification entry in the drawer, you’ll be taken to a trivial activity displaying a message—though in a real application this activity would do something useful based upon the event that occurred (e.g., take users to the newly arrived mail messages). Clicking on the cancel button, or clicking on the Clear Notifications button in the drawer, will remove the red ball from the status bar. Murphy_2419-8C32.fm Page 290 Tuesday, May 5, 2009 9:38 AM ■ ■ ■ PART 6 Other Android Capabilities Murphy_2419-8C33.fm Page 291 Monday, May 4, 2009 3:06 PM [...]... OverlayItem(getPoint(40.74 896 3847316034, -73 .96 807 193 756104), "UN", "United Nations")); items.add(new OverlayItem(getPoint(40.76866 299 974387, -73 .98 268461227417), "Lincoln Center", "Home of Jazz at Lincoln Center")); items.add(new OverlayItem(getPoint(40.765136435316755, -73 .97 9 895 114 898 68), "Carnegie Hall", "Where you go with practice, practice, practice")); items.add(new OverlayItem(getPoint(40.70686417 491 799 , -74.0157 294 2733765),... . bar. Murphy_24 19- 8C32.fm Page 290 Tuesday, May 5, 20 09 9:38 AM ■ ■ ■ PART 6 Other Android Capabilities Murphy_24 19- 8C33.fm Page 291 Monday, May 4, 20 09 3:06 PM Murphy_24 19- 8C33.fm Page 292 Monday,. in greater detail in Chapter 37. Murphy_24 19- 8C33.fm Page 297 Monday, May 4, 20 09 3:06 PM Murphy_24 19- 8C33.fm Page 298 Monday, May 4, 20 09 3:06 PM 299 ■ ■ ■ CHAPTER 34 Mapping with MapView and. encoding="utf-8"?> <RelativeLayout xmlns :android= "http://schemas .android. com/apk/res /android& quot; android: layout_width="fill_parent" android: layout_height="fill_parent"> <com.google .android. maps.MapView