1 / 143

Mobile Programming Lecture 8

Mobile Programming Lecture 8. Notifications, Services, AsyncTask. Lecture 7 Review. How and where in your code do you distinguish between multiple Dialogs in an Activity? How and where in your code do you determine which Menu Option was clicked?

talasi
Download Presentation

Mobile Programming Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mobile Programming Lecture 8 Notifications, Services, AsyncTask

  2. Lecture 7 Review • How and where in your code do you distinguish between multiple Dialogs in an Activity? • How and where in your code do you determine which Menu Option was clicked? • After creating an Options Menu, can you use the same XML file to create a Context Menu?

  3. Lecture 7 Review • How can you edit SharedPreferences? • What's the difference between calling • getSharedPreferences(String, int) and • getDefaultSharedPreferences(Context); • How can you tell when Preferences have been changed at runtime?

  4. Agenda • NotificationManager • Notification • PendingIntent • Service • Communicating with Service • MediaPlayer • AsyncTask

  5. Notifications • Notifications give you the ability to let the user know directly about what work has been completed • —Send a notification that the file has started downloading • After the file completes the service can send out a notification saying that the file has been downloaded successfully.

  6. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); }

  7. Although Notifications are usually sent from Services, we send it from an Activity since that's what you know at this point Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); }

  8. Notifications This will be used as an identifier for this notification unique within this application. public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); }

  9. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } You must use the NotificationManager if you want to send a Notification

  10. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Let's create a new Notification

  11. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } First argument is an icon which should be in res/drawable. I reused the launcher icon here because I'm lazy

  12. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } This is the message that pops up in the status bar when the Notification is delivered. The user doesn't see this message again after that

  13. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } This is the time that will be shown in the Notification, not the time that the Notification will be delivered.

  14. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Let's set up an Intent for launching a new Activity

  15. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Since we don't control the NotificationManager, we need to supply it with our Intent so that it can act on our behalf

  16. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } In this case, we want it to launch our second Activity, so we provide it with an Intent that does just that

  17. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Let's add information for our Notification

  18. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } We need the Context of our application

  19. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } A Title that the user will see in the Notification until it dissappears

  20. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } A message that appears below the Title

  21. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Our PendingIntent to be launched when the Notification is clicked

  22. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } You can add flags to your Notification. Use | to add multiple flags

  23. Notifications public class NotificationExample extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final int NOTIFICATION_ID = 1; NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, "Hi There", System.currentTimeMillis()); Intent notificationIntent = new Intent(this, SecondActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, "You have been notified", "Click to launch second Activity", contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(NOTIFICATION_ID, notification); } Notify the user! You need to supply an id (int) unique within this application, and the Notification itself

  24. Notifications • See NotificationExample.tar • Note that this way of doing it is deprecated • Android 3.0 and higher can still use it, but should use Notification.Builder instead • http://developer.android.com/training/notify-user/build-notification.html

  25. Android Application Components • Activity • Broadcast Receiver • Content Provider • Service

  26. Service - What is it? • a Service is an application component representing an application's desire to • perform a longer-running operation while not interacting with the user • supply functionality for other applications to use • a Service is a facility for the application to • tell the system about something it wants to be doing in the background • expose some of its functionality to other applications

  27. Service - What is it NOT? • —a Service is not a separate process. • it does not imply it is running in its own process • it runs in the same process as the application it is in unless otherwise specified • —a Service is not a thread • it is not a means itself to do work off the main thread

  28. Service - Why use Services? • —For functions that do not require access to an activity’s UI • Performing operations that need to continue even after the application’s activities are not visible • —Long Downloads (Android Market) • —Playing Music (Pandora) • —Performing operations that need to exist regardless of activities coming and going • —Maintaining a connection for a chat application • —Performing periodic work without user intervention. • —Updating the Weather Widget.

  29. Service - Process LifeCycle Android will attempt to keep the process hosting a service around as long as the service has been started or has clients bound to it • If the service is currently executing code in its onCreate(), onStartCommand(), or onDestroy() methods, then the hosting process will be a foreground process to ensure this code can execute without being killed.

  30. Service - Forms of a Service A service can take 2 forms • A Started Service is one where an application component (such as an Activity) starts it by calling startService() • it can then run in the background indefinitely, even if the component that started it has been destroyed • onStartCommand() allows it to start • A Bound Service is one where an application component binds to it by calling bindService() • it then offers a client-server interface that allows components to interact with it • a bound service runs only as long as an another application component is bound to it • multiple component can bind to the service at the same time • onBind() allows binding on the Service

  31. Service - LifeCycle This is important

  32. Service - LifeCycle in either case ... • onCreate() • not called if Service is already running • onStartCommand() • service now started and can run in background indefinitely • onDestroy() • Service is no longer in use and is being destroyed

  33. Adding a Service to your Project • Open AndroidManifest.xml • Click on the Application Tab • Under Application Nodes, click Add ... • Select Service • Under Attributes for Service, click on Name* • Enter the name of your Service, e.g. MyService • Your class may be underlined in red, mouse over it and choose Add unimplemented methods

  34. Creating a Started Service Override the Service method: public int onStartCommand(Intent, int, int) • to stop the Service (you should when it's job is done) • the Service should stop itself by calling stopSelf() when it's job is done • another component can call stopService(Intent)

  35. Creating a Started Service public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myIntent = new Intent(this, MyService.class); startService(myIntent); } }

  36. Creating a Started Service Here we start with an Activity public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myIntent = new Intent(this, MyService.class); startService(myIntent); } }

  37. Creating a Started Service public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myIntent = new Intent(this, MyService.class); startService(myIntent); } } Looks familiar? Starting a Service is almost the same as starting an Activity

  38. Creating a Started Service public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myIntent = new Intent(this, MyService.class); startService(myIntent); } } We call startService() instead of startActivity() thoguh

  39. Creating a Started Service Here we extend Service public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "Service.onStartCommand()"); final int LIMIT = 10; int i; // do nothing for LIMIT iterations for(i = 0; i < LIMIT; i++) ; stopSelf(); return Service.START_STICKY; } }

  40. Creating a Started Service We override onStartCommand(), which is called each time startService() is called public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "Service.onStartCommand()"); final int LIMIT = 10; int i; // do nothing for LIMIT iterations for(i = 0; i < LIMIT; i++) ; stopSelf(); return Service.START_STICKY; } }

  41. Creating a Started Service public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId){ Log.i(TAG, "Service.onStartCommand()"); final int LIMIT = 10; int i; // do nothing for LIMIT iterations for(i = 0; i < LIMIT; i++) ; stopSelf(); return Service.START_STICKY; } } Note the semicolon. This for loop is empty and is just there to simulate the Service doing some work

  42. Creating a Started Service public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId){ Log.i(TAG, "Service.onStartCommand()"); final int LIMIT = 10; int i; // do nothing for LIMIT iterations for(i = 0; i < LIMIT; i++) ; stopSelf(); return Service.START_STICKY; } } The Service is done so it stops itself

  43. Creating a Started Service public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId){ Log.i(TAG, "Service.onStartCommand()"); final int LIMIT = 10; int i; // do nothing for LIMIT iterations for(i = 0; i < LIMIT; i++) ; stopSelf(); return Service.START_STICKY; } } onStartCommand() must return a value, although this return value may be useless here since we called stopSelf()

  44. Creating a Started Service See ServiceLifeCycleExample.tar

  45. Creating a Started Service onStartCommand() return values • START_STICKY • if this service's process is killed while it is started (after returning from onStartCommand() ), then leave it in the started state. • This is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job • START_NOT_STICKY • if this service's process is killed while it is started, then take the service out of the started state and don't recreate. • START_REDELIVER_INTENT • if this service's process is killed while it is started (after returning from onStartCommand()), then it will be scheduled for a restart and the last delivered Intent re-delivered to it

  46. Creating a Started Service To create a started service, you can either extend Service or IntentService • Service • it uses your main application's thread by default and can slow down your app • you should create a new thread for the Service to work in if it slows down your app • IntentService • ...

  47. IntentService If you don't want to create a separate thread yourself, you can extend IntentService, which • creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread • creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading. • stops the service after all start requests have been handled, so you never have to call stopSelf(). • provides default implementation of onBind() that returns null. • provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.

  48. IntentService In other words • You really only need a • constructor • to override onHandleIntent(Intent) If you decide to override onCreate() or onStartCommand() or onDestroy() anyway, then you should call super.on_()

  49. IntentService public class MyService extends IntentService { private static final String TAG = "ServiceExample"; public MyService() { super("MyService"); } @Override protected void onHandleIntent(Intent intent) { final int LIMIT = 200000000; int i; for(i = 0; i < LIMIT; i++) ; } }

  50. Communicating with a Service • Typically you would like to tell the application about events that occursinside a service. • The service client (e.g. Activity) can provide some sort of “callback” or “listener” object to the service, which the service could then call when needed.

More Related