亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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配置
復(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);
}
}

相關(guān)文章

最新評論