freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

第5章android服務(wù)(service)-文庫吧

2025-08-25 16:01 本頁面


【正文】 eState) { (savedInstanceState)。 setContentView()。 start=(Button)findViewById()。 stop=(Button)findViewById()。 final Intent intent=new Intent()。 ()。 (new OnClickListener() { public void onClick(View v) { startService(intent)。 } })。 (new OnClickListener() { public void onClick(View v) { stopService(intent)。 } })。 } } 案例 :FirstService 案例 :FirstService 運(yùn)行本節(jié)的例子后,第一次單擊【啟動(dòng) Service】按鈕后,在 DDMS視圖下的LogCat視圖有如下圖所示的輸出。 然后單擊【關(guān)閉 Service】按鈕, LogCat視圖有如下圖所示的輸出。 下面按如下的單擊順序,重新測(cè)試一下本例。 【啟動(dòng) Service】 → 【啟動(dòng) Service】 → 【啟動(dòng) Service】 → 【停止 Service】 測(cè)試完程序 , 查看 LogCat控制臺(tái)輸出信息如下圖所示 。 系統(tǒng)只在第一次單擊【 啟動(dòng) Service】 按鈕時(shí)調(diào)用 onCreate()和 onStartCommand()方法 , 再單擊該按鈕 時(shí) , 系統(tǒng)只會(huì)調(diào)用 onStartCommand()方法 , 而不會(huì)重復(fù)調(diào)用 onCreate()方法 。 綁定 Service過程 Context的 bindService()方法的完整方法簽名為: bindService( Intent service,ServiceConnection conn, int flags), 該方法的三個(gè)參數(shù)解釋如下: ?service: 該參數(shù)表示與服務(wù)類相關(guān)聯(lián)的 Intent對(duì)象,用于指定所綁定的 Service應(yīng)該符合哪些條件; ?conn: 該參數(shù)是一個(gè) ServiceConnection對(duì)象,該對(duì)象用于監(jiān)聽訪問者與 Service之間的連接情況。當(dāng)訪問者與 Service之間連接成功時(shí),將回調(diào)該 ServiceConnection對(duì)象的 onServiceConnected( ComponentName name,IBinder service)方法;當(dāng)訪問者與Service之間斷開連接時(shí)將回調(diào)該 ServiceConnection對(duì)象的onServiceDisconnected( ComponentName name)方法。 ?flags: 指定綁定時(shí)是否自動(dòng)創(chuàng)建 Service(如果 Service還未創(chuàng)建)。該參數(shù)可指定 BIND_AUTO_CREATE(自動(dòng)創(chuàng)建 )。 ? 當(dāng)開發(fā) Service類時(shí),該 Service類必須提供一個(gè) onBind()方法,在綁定本地 Service的情況下, onBind()方法所返回的 IBinder對(duì)象將會(huì)傳給 ServiceConnection對(duì)象里 onServiceConnected( ComponentName name,IBinder service)方法的 service參數(shù),這樣訪問者就可以通過 IBinder對(duì)象與 Service進(jìn)行通信。 ? 實(shí)際開發(fā)時(shí)通常會(huì)采用繼承 Binder( IBinder的實(shí)現(xiàn)類)的方式實(shí)現(xiàn)自己的 IBinder對(duì)象。 綁定 Service過程 綁定 Service案例 在上一個(gè)案例中 , 添加兩個(gè)按鈕 , 一個(gè)用于綁定服務(wù) , 一個(gè)用于解綁 , 然后分別為其添加事件處理 。 在綁定服務(wù)時(shí) , 需要傳遞一個(gè) ServiceConnection對(duì)象 , 所以先創(chuàng)建該對(duì)象 , 代碼如下 。 private ServiceConnection conn=new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { (TAG,MainActivity onServiceDisconnected invoked!)。 } public void onServiceConnected(ComponentName name, IBinder service) { (TAG,MainActivity onServiceConnected invoked!)。 myBinder=(MyBinder)service。 } }。 程序清單: /FirstService/src/iet/jxufe//android/ (綁定 Service案例) 然后在 MyService中添加兩個(gè)與綁定 Service相關(guān)的方法 , onUnBind()和onRebind(),與其他方法類似 , 只是在方法體重打印出該方法被調(diào)用了信息 , 代碼如下 。 public boolean onUnbind(Intent intent) { (TAG, MyService onUnbind invoked!)。 return (intent)。 } public void onRebind(Intent intent) { (TAG, MyService onRebind invoked!)。 (intent)。 } 程序清單: /FirstService/src/iet/jxufe//android/ (綁定 Service案例) 最后在 MainActivity中為綁定 Service和解綁 Service按鈕添加事件處理,代碼如下。 public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { (savedInstanceState)。 setContentView()。 bind=(Button)findViewById()。 unbind=(Button)findViewById()。 final Intent intent=new Intent()。 ()。 (new OnClickListener() { public void onClick(View v) { bindService(intent, conn,)。 } })。 (new OnClickListener() { public void onClick(View v) { unbindService(conn)。 } })。 程序清單: /FirstService/src/iet/jxufe//android/ (綁定 Service案例) 程序執(zhí)行后,單擊綁定 Service按鈕, LogCat控制臺(tái)打印信息如下圖所示,首先調(diào)用 onCreate()方法,然后調(diào)用 onBind()方法。 程序執(zhí)行后,單擊解綁 Service按鈕, LogCat控制臺(tái)打印信息如下圖所示,首先調(diào)用 onUnbind()方法,然后調(diào)用 onDestory()方法。 程序運(yùn)行后,單擊綁定 Service按鈕,然后退出程序, LogCat控制臺(tái)打印信息如下圖。可以看出,程序退出后, Service會(huì)自動(dòng)退出。 (綁定 Service案例) 當(dāng)單擊綁定 Service按鈕后 , 再重復(fù)多次單擊綁定 Service按鈕 , 查看控制臺(tái)打印信息 , 發(fā)現(xiàn) 程序并不會(huì)多次調(diào)用 onBind()方法 。 采用綁定服務(wù) 的 另一個(gè)優(yōu)勢(shì)是組件可以與 Service之間進(jìn)行通信 , 傳遞數(shù)據(jù) 。 這主要是 通過 IBinder對(duì)象進(jìn)行 的 , 因此需在 Service中創(chuàng)建一個(gè)IBinder對(duì)象 , 然后讓其作為 onBind()方法的返回值返回 , 對(duì)數(shù)據(jù)的操作是放在 IBinder對(duì)象中的 。 修改我們的 MyService類 , 添加一個(gè)內(nèi)部類MyBinder, 同時(shí)在 onCreate()方法中啟動(dòng)一個(gè)線程 , 模擬后臺(tái)服務(wù) , 該線程主要是做數(shù)據(jù)遞增操作 , 在 MyBinder類中 , 提供一個(gè)方法 , 可以獲取當(dāng)前遞增的值 ( count的值 ) , 具體代碼如下 。 程序清單: /FirstService/src/iet/jxufe//android/ (綁定 Service案例) public class MyService extends Service { private static final String TAG = MyService。 private int count=0。 private boolean quit=false。 private MyBinder myBinder=new MyBinder()。 public class MyBinder extends Binder { publi
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1