• This type of service, is created and started when a component such as an activity calls the startService method to start it.. • Which is a service that does not return a result to the[r]
Trang 1SERVICE AND NOTIFICATION
LECTURER: VÕ TẤN DŨNG
SUBJECT: MOBILE DEVICE PROGRAMMING
(ANDROID)
Trang 2APPLICATION
Trang 3HOW TO WORK WITH THE APPLICATION OBJECT
Description:
• To store data and methods that apply to the entire application, you can extend the Application class and add instance variables and methods The Application object is created when the app starts and remains available until the app ends.
• To run code only once when the application start, you can override the onCreate method of your custom Applications class.
Trang 4Starting point for the NewsReaderApp class
Trang 5HOW TO REGISTER THE APPLICATION OBJECT
Trang 8SERVICE
Trang 9HOW TO WORK WITH SERVICES
Trang 10UNBOUND SERVICE
Description:
• This type of service, is created and started when a component such as an activity calls the startService method to start it.
• Which is a service that does not return a result to the caller.
• The service runs until it’s stopped.
Unbound service is stopped in one of three ways:
• First, another component such as an activity can call the stopService method to stop it.
• Second, the service can call the stopSelf method to stop it itself.
• Third, Android can stop the service when the user turns off the device runs low on
memory.
Trang 11BOUND SERVICE
Description:
• Which is a service that interact with one or more components.
• This type of service is started when a component calls the bindService method to bind to it.
• Multi components can bind to the service at the same time.
• When all of them unbind, the service is destroyed.
Trang 12THE LIFECYCLE OF A SERVICE
Trang 13HOW TO CREATE A SERVICE
(UNBOUND SERVICE)
Three classes are needed to implement a service: Service, Intent and IBinder
A service implements four methods that you typically need for an unbound service
• onCreate method is called when the service is first created It is a good place to put code that’s only executed multiple times
• onStartCommand method is executed each time a component uses the startService method to start this service This method return one of three constants:
Trang 14THREE CONSTANTS OF THE onStartCommand()
Trang 16HOW TO REGISTER A SERVICE
Trang 17HOW TO START AND STOP A SERVICE
Trang 18HOW TO STOP AND STOP A SERVICE
Trang 19HOW TO USED THREADS WITH SERVICES
Trang 22HOW TO TEST A SERVICE
Trang 23HOW TO VIEW ALL SERVICES
Trang 25NOTIFICATION
Trang 26HOW TO WORK WITH NOTIFICATIONS
Trang 27• Notification
• Notification area
• Notification drawer
Trang 28PENDING INTENT
Trang 29HOW TO CREATE AN PENDING INTENT
Trang 32HOW TO CREATE A NOTIFICATION
Trang 34HOW TO WORK WITH SYSTEM SERVICES
Trang 37HOW TO CHECK IF A NETWORK CONNECTION IS AVAILABLE
Trang 40THE NEWS READER APP
Read sample code in “Murach’s Android Programming”
(form page396 to page 405)
• Download, Upload feeds
• Display a notification whenever an updated feed is available
• Add a custom Application object to store the milliseconds for the current news feed and to start the service (to control which is the new feed and which is the old feed)
Trang 41CLASSES OF NEWS READER APP
Trang 42END