SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Android Application Development Service Ahsanul Karim ahsanul.karim@sentinelbd.com Sentinel Solutions Ltd. http://www.sentinelbd.com
Application Building Blocks
Android Application Anatomy Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extendsthe Activity Base class Services NoUser Interface Runs in Background Extends the Service Base Class Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity Extends the BroadcastReceiver Base Class Content Provider Makes application data available to other apps Data stored in SQLite database Extends the ContentProviderBase class Application= Set of Android Components
Service What is Service? Services are codes that run in the background They can be started and stopped Services doesn’t have UI startService() Audio Service Plays Audio stopService() Audio Player UI
Service What a Service is NOT? There are some confusions: 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).  Main Features of Service 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) to calls to Context.startService(), which ask the system to schedule work for the service,  to be run until the service or someone else explicitly stop it.
Service Service Example We’ll create a simple ServiceDemo application which runs in background and shows  notification in the upper Notification Bar with a period of specified time We create a project with following: Project Name: ServiceDemo Build Target: 1.6 Application name:ServiceDemo Package name:  com.basistraining.servicedemo Create Activity: ServiceDemoActivity Min SDK Version: 4
Service Service Example (Contd.) 2. We’ll add a new class MyServicethat extends Service. We get the following. 3. We’ll also need to add the Service in AndroidMenifest.xml 4. Now we add other lifecycle methods of the MyService:  onCreate() onStart() onDestroy()
Service Service Example (Contd.)
Service Service Example (Contd.)
Service Service Example (Contd.) Now let’s make the Layout res/layout/main.xml to have 2 buttons to start and stop the  Service There are only 2 buttons with id “@+id/btnStart” and“@+id/btnStop”
Service Service Example (Contd.) Now we add action to our Buttons to Start or Stop the MyService and the Application in our  onCreate() method of the Activity
Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
Service Service Example (Contd.) Now to do something on Starting of our Service, we do following: Now to stop the timer, we do following:
Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
Service Service Example (Contd.) Now, Let we want to show a notification in the Notification Bar instead of just LogCat
Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in  Notification

Weitere ähnliche Inhalte

Was ist angesagt?

Android activity
Android activityAndroid activity
Android activity
Krazy Koder
 
Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialog
Krazy Koder
 
Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 

Was ist angesagt? (20)

Android activity
Android activityAndroid activity
Android activity
 
Fragment
Fragment Fragment
Fragment
 
Database in Android
Database in AndroidDatabase in Android
Database in Android
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android ui dialog
Android ui dialogAndroid ui dialog
Android ui dialog
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Broadcast Receiver
Broadcast ReceiverBroadcast Receiver
Broadcast Receiver
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 
Android notification
Android notificationAndroid notification
Android notification
 
Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Model View Controller (MVC)
Model View Controller (MVC)Model View Controller (MVC)
Model View Controller (MVC)
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Notification android
Notification androidNotification android
Notification android
 
Servlets
ServletsServlets
Servlets
 
05 intent
05 intent05 intent
05 intent
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Android service
Android serviceAndroid service
Android service
 

Andere mochten auch

Android before getting started
Android before getting startedAndroid before getting started
Android before getting started
Ahsanul Karim
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities
Ahsanul Karim
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
Ahsanul Karim
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
Ahsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS Tutorial
Ahsanul Karim
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
Ahsanul Karim
 
Sensors in Android (old)
Sensors in Android (old)Sensors in Android (old)
Sensors in Android (old)
Ahsanul Karim
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivity
Ahsanul Karim
 
Day 2 android internals a quick overview
Day 2 android internals a quick overviewDay 2 android internals a quick overview
Day 2 android internals a quick overview
Ahsanul Karim
 

Andere mochten auch (20)

Day 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver ComponentDay 6: Android BroadcastReceiver Component
Day 6: Android BroadcastReceiver Component
 
Client-Server
Client-ServerClient-Server
Client-Server
 
Android before getting started
Android before getting startedAndroid before getting started
Android before getting started
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2
 
GCM for Android
GCM for AndroidGCM for Android
GCM for Android
 
Day1 before getting_started
Day1 before getting_startedDay1 before getting_started
Day1 before getting_started
 
Lecture 3 getting active through activities
Lecture 3 getting active through activities Lecture 3 getting active through activities
Lecture 3 getting active through activities
 
Mcq peresentation
Mcq  peresentationMcq  peresentation
Mcq peresentation
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location API
 
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through ActivitiesDay 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
 
Android 1.8 sensor
Android 1.8 sensorAndroid 1.8 sensor
Android 1.8 sensor
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Android Workshop: Day 1 Part 3
Android Workshop: Day 1 Part 3Android Workshop: Day 1 Part 3
Android Workshop: Day 1 Part 3
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS Tutorial
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
 
Sensors in Android (old)
Sensors in Android (old)Sensors in Android (old)
Sensors in Android (old)
 
Training android
Training androidTraining android
Training android
 
Android MapView and MapActivity
Android MapView and MapActivityAndroid MapView and MapActivity
Android MapView and MapActivity
 
Day 2 android internals a quick overview
Day 2 android internals a quick overviewDay 2 android internals a quick overview
Day 2 android internals a quick overview
 

Ähnlich wie Android Services

Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in Background
Ahsanul Karim
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1
Utkarsh Mankad
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
Ahsanul Karim
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
Ahsanul Karim
 
Part 1 implementing a simple_web_service
Part 1 implementing a simple_web_servicePart 1 implementing a simple_web_service
Part 1 implementing a simple_web_service
krishmdkk
 
Multiple Activity and Navigation Primer
Multiple Activity and Navigation PrimerMultiple Activity and Navigation Primer
Multiple Activity and Navigation Primer
Ahsanul Karim
 

Ähnlich wie Android Services (20)

Day 15: Working in Background
Day 15: Working in BackgroundDay 15: Working in Background
Day 15: Working in Background
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)
 
Services I.pptx
Services I.pptxServices I.pptx
Services I.pptx
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1
 
Fundamentals & Services
Fundamentals & ServicesFundamentals & Services
Fundamentals & Services
 
Unit2
Unit2Unit2
Unit2
 
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxxMAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
MAD Unit 5.pptxxxxxxxxxxxxxxxxxxxxxxxxxx
 
Android101
Android101Android101
Android101
 
AIDL - Android Interface Definition Language
AIDL  - Android Interface Definition LanguageAIDL  - Android Interface Definition Language
AIDL - Android Interface Definition Language
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Day 3: Getting Active Through Activities
Day 3: Getting Active Through ActivitiesDay 3: Getting Active Through Activities
Day 3: Getting Active Through Activities
 
Mobile testing android
Mobile testing   androidMobile testing   android
Mobile testing android
 
Background Tasks with Worker Service
Background Tasks with Worker ServiceBackground Tasks with Worker Service
Background Tasks with Worker Service
 
Android 8 behavior changes
Android 8 behavior changesAndroid 8 behavior changes
Android 8 behavior changes
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
Nativa Android Applications development
Nativa Android Applications developmentNativa Android Applications development
Nativa Android Applications development
 
Part 1 implementing a simple_web_service
Part 1 implementing a simple_web_servicePart 1 implementing a simple_web_service
Part 1 implementing a simple_web_service
 
Android Basic- CMC
Android Basic- CMCAndroid Basic- CMC
Android Basic- CMC
 
cpuk10745
cpuk10745cpuk10745
cpuk10745
 
Multiple Activity and Navigation Primer
Multiple Activity and Navigation PrimerMultiple Activity and Navigation Primer
Multiple Activity and Navigation Primer
 

Mehr von Ahsanul Karim

Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
Ahsanul Karim
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
Ahsanul Karim
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
Ahsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
Ahsanul Karim
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
Ahsanul Karim
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
Ahsanul Karim
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)
Ahsanul Karim
 

Mehr von Ahsanul Karim (16)

Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & Preferences
 
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
 
লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
Day 4: Android: UI Widgets
Day 4: Android: UI WidgetsDay 4: Android: UI Widgets
Day 4: Android: UI Widgets
 
Day 1 Android: Before Getting Started
Day 1 Android: Before Getting StartedDay 1 Android: Before Getting Started
Day 1 Android: Before Getting Started
 
Mobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyMobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete Study
 
List Views
List ViewsList Views
List Views
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)
 
AndroidManifest
AndroidManifestAndroidManifest
AndroidManifest
 
Action Bar Sherlock tutorial
Action Bar Sherlock tutorialAction Bar Sherlock tutorial
Action Bar Sherlock tutorial
 
Introduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting StartedIntroduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting Started
 

Kürzlich hochgeladen

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Android Services

  • 1. Android Application Development Service Ahsanul Karim ahsanul.karim@sentinelbd.com Sentinel Solutions Ltd. http://www.sentinelbd.com
  • 3. Android Application Anatomy Activities Provides User Interface Usually represents a Single Screen Can contain one/more Views Extendsthe Activity Base class Services NoUser Interface Runs in Background Extends the Service Base Class Intent/Broadcast Receiver Receives and Reacts to broadcast Intents No UI but can start an Activity Extends the BroadcastReceiver Base Class Content Provider Makes application data available to other apps Data stored in SQLite database Extends the ContentProviderBase class Application= Set of Android Components
  • 4. Service What is Service? Services are codes that run in the background They can be started and stopped Services doesn’t have UI startService() Audio Service Plays Audio stopService() Audio Player UI
  • 5. Service What a Service is NOT? There are some confusions: 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). Main Features of Service 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) to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
  • 6. Service Service Example We’ll create a simple ServiceDemo application which runs in background and shows notification in the upper Notification Bar with a period of specified time We create a project with following: Project Name: ServiceDemo Build Target: 1.6 Application name:ServiceDemo Package name: com.basistraining.servicedemo Create Activity: ServiceDemoActivity Min SDK Version: 4
  • 7. Service Service Example (Contd.) 2. We’ll add a new class MyServicethat extends Service. We get the following. 3. We’ll also need to add the Service in AndroidMenifest.xml 4. Now we add other lifecycle methods of the MyService: onCreate() onStart() onDestroy()
  • 10. Service Service Example (Contd.) Now let’s make the Layout res/layout/main.xml to have 2 buttons to start and stop the Service There are only 2 buttons with id “@+id/btnStart” and“@+id/btnStop”
  • 11. Service Service Example (Contd.) Now we add action to our Buttons to Start or Stop the MyService and the Application in our onCreate() method of the Activity
  • 12. Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  • 13. Service Service Example (Contd.) Now to do something on Starting of our Service, we do following: Now to stop the timer, we do following:
  • 14. Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in LogCat
  • 15. Service Service Example (Contd.) Now, Let we want to show a notification in the Notification Bar instead of just LogCat
  • 16. Service Service Example (Contd.) If we run the app and test we’ll see our buttons are starting and stopping the service in Notification