国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android的Service應用程序組件基本編寫方法

2020-04-11 12:38:58
字體:
來源:轉載
供稿:網友
Service是什么
Service是一個android 系統中的應用程序組件,它跟Activity的級別差不多,但是他沒有圖形化界面,不能自己運行,只能后臺運行,并且可以和其他組件進行交互如更新ContentProvider,Intent以及系統的通知等等。其啟動方式有兩種:context.startService() 和 context.bindService()。Service通常用來處理一些耗時比較長的操作。

Service的編寫
創建一個類(這里為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);
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄骅市| 和顺县| 弥勒县| 安塞县| 兴城市| 孟村| 进贤县| 大田县| 宜黄县| 拜城县| 土默特左旗| 宝应县| 郧西县| 云阳县| 古蔺县| 鹤峰县| 嘉鱼县| 福建省| 虎林市| 陇南市| 南昌市| 嘉黎县| 长乐市| 恩施市| 甘德县| 长泰县| 金沙县| 遂昌县| 呈贡县| 奈曼旗| 星子县| 泉州市| 安徽省| 高安市| 益阳市| 四会市| 阳江市| 大关县| 汾阳市| 马公市| 淮南市|