Android的Service應(yīng)用程序組件基本編寫方法
Service是一個(gè)android 系統(tǒng)中的應(yīng)用程序組件,它跟Activity的級(jí)別差不多,但是他沒(méi)有圖形化界面,不能自己運(yùn)行,只能后臺(tái)運(yùn)行,并且可以和其他組件進(jìn)行交互如更新ContentProvider,Intent以及系統(tǒng)的通知等等。其啟動(dòng)方式有兩種:context.startService() 和 context.bindService()。Service通常用來(lái)處理一些耗時(shí)比較長(zhǎng)的操作。
Service的編寫
創(chuàng)建一個(gè)類(這里為FirstService)繼承android.app.Service,并覆蓋以下方法:
onBind(Intent intent) Return the communication channel to the service.
onCreate() Called by the system when the service is first created.
onStartCommand(Intent intent, int flags, int startId) Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request.
onDestroy() Called by the system to notify a Service that it is no longer used and is being removed.
AndroidManifest.xml文件中添加service配置
<service android:name=".FirstService"></service>
在Activity中啟動(dòng)和停止Service的點(diǎn)擊事件的編寫
class StartServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
startService(intent);
}
}
class StopServiceListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(TestActivity.this, FirstService.class);
stopService(intent);
}
}
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- Android開發(fā)中Button組件的使用
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說(shuō)明
- android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤
- Android中butterknife的使用與自動(dòng)化查找組件插件詳解
- Android開發(fā)之組件GridView簡(jiǎn)單使用方法示例
- Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- Android UI新組件學(xué)習(xí)和使用
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無(wú)法接收到Touch事件的問(wèn)題
這篇文章主要介紹了詳解 Android中Libgdx使用ShapeRenderer自定義Actor解決無(wú)法接收到Touch事件的問(wèn)題的相關(guān)資料,希望通過(guò)本文能幫助到大家解決這樣的問(wèn)題,需要的朋友可以參考下2017-09-09
Android Navigation TabBar控件實(shí)現(xiàn)多彩標(biāo)簽欄
這篇文章主要為大家詳細(xì)介紹了Android Navigation TabBar控件實(shí)現(xiàn)多彩標(biāo)簽欄的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-05-05
Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要介紹了Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,簡(jiǎn)單分析了基于CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)功能的技巧,需要的朋友可以參考下2015-12-12
Android setButtonDrawable()的兼容問(wèn)題解決辦法
這篇文章主要介紹了Android setButtonDrawable()的兼容問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android應(yīng)用程序簽名步驟及相關(guān)知識(shí)介紹
本文主要介紹Android應(yīng)用程序簽名相關(guān)的理論知識(shí),包括:什么是簽名、為什么要給應(yīng)用程序簽名、如何給應(yīng)用程序簽名等,感興趣的朋友可以參考下哈2013-04-04
Android?鼠標(biāo)光標(biāo)的圖形合成原理實(shí)例探究
這篇文章主要為大家介紹了Android?鼠標(biāo)光標(biāo)的圖形合成原理實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Android在fragment中編寫toobar的步驟詳解
這篇文章主要介紹了Android在fragment中編寫toobar,本文分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android如何通過(guò)手機(jī)獲取驗(yàn)證碼來(lái)完成注冊(cè)功能
注冊(cè)app或者網(wǎng)絡(luò)帳號(hào)的時(shí)候,經(jīng)常需要手機(jī)獲取驗(yàn)證碼,來(lái)完成注冊(cè)功能,那么android如何通過(guò)手機(jī)獲取驗(yàn)證碼來(lái)完成注冊(cè)功能,代碼是怎么實(shí)現(xiàn)的呢?下面看看小編給大家?guī)Я说囊欢未a分析吧2015-11-11

