SlideShare a Scribd company logo
1 of 23
第八堂:
學習使用 Service
柯力中 Jason Ko
Jason的Android 快樂應用程式學習班
課程內容
• Service 服務
• Thread 和 Handler
• Toast
• AlertDialog
• Notification
• 利用 Service 組合做出 Timer 計時器
Jason的Android 快樂應用程式學習班
Service 服務
Jason的Android 快樂應用程式學習班
Service 服務
• Service 是 Android 的主要建構單元之一, 和 Activity
不同, Service 不會有使用者介面, 是背景執行一段程
式碼
• Service 在背景執行, 不受 Activity 影響
• Service 可分為 bound service 跟 unbound service 兩
種
• unbound service 只有開始跟停止兩個狀態
Jason的Android 快樂應用程式學習班
Service 的 Lifecycle
Jason的Android 快樂應用程式學習班
IntentService
Jason的Android 快樂應用程式學習班
• IntentService 是另外開一條 Thread
• IntentService 同一時間只能有一個在跑
• 因為 IntentService 不在 main thread 上, 可以
用來下載網路資料
ex. 利用 Service 每隔一秒輸出文字
Jason的Android 快樂應用程式學習班
1. 建立一個 Project, 名稱為 ServiceTimer
2. 在 MainActivity 做一個開始 button 與一個停止 button
3. 新建一個 TimerService extends IntentService
4. 當按下開始 button 時, 每隔一秒在 Log 輸出文字”still running”, 當
按下停止 button 時, 停止輸出文字
開始
停止
Log
still running…
still running…
still running…
Thread 與 Handler
Jason的Android 快樂應用程式學習班
Thread 與 Handler
Jason的Android 快樂應用程式學習班
MainThread
Thread
報告, 處理好了,
需要更新 UI
Handler
交給我就好,
我來處理
ex. 改變按鈕狀態
Jason的Android 快樂應用程式學習班
1. 將兩個 button 變成一個 button
2. 當 service 在跑時, button 文字為停止
3. 當輸出十個 “still running” 後, 停止 Service, 並變更
button 文字為 開始
停止
Log
still running…
still running…
still running…
Toast
Jason的Android 快樂應用程式學習班
Toast
Jason的Android 快樂應用程式學習班
• 短暫浮出的訊息框, 用意是提醒使用者, 不需做任何動
作
ex. 倒數結束時, 顯示 Toast 訊息
Jason的Android 快樂應用程式學習班
1. 當跑完 10 秒後, Toast 顯示 “倒數結束!”
開始
倒數結束!
AlertDialog
Jason的Android 快樂應用程式學習班
AlertDialog
Jason的Android 快樂應用程式學習班
still running…
• AlertDialog 是一個彈出的視窗 (對話框), 用意是讓
使用者決定接下來的動作
setTitle()
setView()
setPositiveButton(),
setNegativeButton()
ex. 建立一個 TextView, 點及時彈出設置秒
數的 AlertDialog
Jason的Android 快樂應用程式學習班
1. 建立一個 AlertDialog 需要的 layout, 取名為
custom_dialog
2. 在 MainActivity 中 new AlertDialog.Builder()
3. 利用 LayoutInflater 取得 custom_dialog
4. setTitle(), setView(), setPositiveButton(),
setNegativeButton()
Notification
Jason的Android 快樂應用程式學習班
Notification
Jason的Android 快樂應用程式學習班
• 顯示在使用者訊息面板的訊息框
Notification
setSmallIcon()
setContentTitle()
setContentText()
ex. 當按下開始的時候, 彈出
Notification, 倒數結束的時候, 結束
Notification
1. 按下開始 Button 時, 產生 Notification
2. 在 TimerService 內, startForeground()
3. 倒數結束時, stopForeground()
P.S stopForeground() 表示移除 service 的 foreground
state, allowing it to be killed if more memory is needed.
利用 Service 組合做出 Timer
Jason的Android 快樂應用程式學習班
利用 Service 組合做出 Timer
• 想像 Timer 的狀態
• 取得系統的時間
• 開始計時時, 彈出 Notification
• 比較經過了多少時間
• 不斷更新 TextView
• 時間結束時, 彈出 Toast
Jason的Android 快樂應用程式學習班
Calendar c = Calendar.getInstance();
int currentSeconds = (int)
(c.getTimeInMillis()/1000);
完整程式碼
Jason的Android 快樂應用程式學習班
https://github.com/KosbrotherSchool/Teach_Timer
Service

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

第八堂 學習使用 Service