Android的Service應(yīng)用程序組件基本編寫方法
更新時間:2012年12月10日 15:55:24 作者:
Service是一個android 系統(tǒng)中的應(yīng)用程序組件,它跟Activity的級別差不多,但是他沒有圖形化界面,不能自己運行,只能后臺運行,Service通常用來處理一些耗時比較長的操作
Service是什么
Service是一個android 系統(tǒng)中的應(yīng)用程序組件,它跟Activity的級別差不多,但是他沒有圖形化界面,不能自己運行,只能后臺運行,并且可以和其他組件進(jìn)行交互如更新ContentProvider,Intent以及系統(tǒng)的通知等等。其啟動方式有兩種:context.startService() 和 context.bindService()。Service通常用來處理一些耗時比較長的操作。
Service的編寫
創(chuàng)建一個類(這里為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中啟動和停止Service的點擊事件的編寫
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);
}
}
Service是一個android 系統(tǒng)中的應(yīng)用程序組件,它跟Activity的級別差不多,但是他沒有圖形化界面,不能自己運行,只能后臺運行,并且可以和其他組件進(jìn)行交互如更新ContentProvider,Intent以及系統(tǒng)的通知等等。其啟動方式有兩種:context.startService() 和 context.bindService()。Service通常用來處理一些耗時比較長的操作。
Service的編寫
創(chuàng)建一個類(這里為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配置
復(fù)制代碼 代碼如下:
<service android:name=".FirstService"></service>
在Activity中啟動和停止Service的點擊事件的編寫
復(fù)制代碼 代碼如下:
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如何實現(xiàn)跨進(jìn)程調(diào)用Service
- Android開發(fā)中Button組件的使用
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說明
- android自定義組件實現(xiàn)儀表計數(shù)盤
- Android中butterknife的使用與自動化查找組件插件詳解
- Android開發(fā)之組件GridView簡單使用方法示例
- Android列表組件ListView使用詳解之動態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- Android UI新組件學(xué)習(xí)和使用
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
android實現(xiàn)在圖標(biāo)上顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)在圖標(biāo)上顯示數(shù)字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android實現(xiàn)頂部導(dǎo)航欄可點擊可滑動效果(仿微信仿豆瓣網(wǎng))
這篇文章主要介紹了 Android實現(xiàn)頂部導(dǎo)航欄可點擊可滑動效果(仿微信仿豆瓣網(wǎng)),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03Android實現(xiàn)Toast提示框圖文并存的方法
這篇文章主要介紹了Android實現(xiàn)Toast提示框圖文并存的方法,實例分析了Toast提示框的參數(shù)設(shè)置及圖文調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android編程基于自定義view實現(xiàn)公章效果示例【附源碼下載】
這篇文章主要介紹了Android編程基于自定義view實現(xiàn)公章效果,結(jié)合實例形式分析了Android使用自定義view進(jìn)行圖形繪制的相關(guān)操作技巧,并附帶完整實例源碼供讀者下載參考,需要的朋友可以參考下2017-11-11Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析講解
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請求和響應(yīng)處理重點分析,在使用?Retrofit發(fā)起網(wǎng)絡(luò)請求時,我們可以通過定義一個接口并使用Retrofit的注解來描述這個接口中的請求,Retrofit會自動生成一個實現(xiàn)該接口的代理對象2023-03-03Android?獲取手機已安裝的應(yīng)用列表實現(xiàn)詳解
這篇文章主要介紹了Android?獲取手機已安裝的應(yīng)用列表的實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08