第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么用 unbindService() 方法解綁定服務(wù)后 仍然可以調(diào)用服務(wù)中的方法?

為什么用 unbindService() 方法解綁定服務(wù)后 仍然可以調(diào)用服務(wù)中的方法?

kokoromi 2016-07-04 20:44:57
?? 為什么我用 unbindService 方法解除了綁定后,仍然可以在 活動中調(diào)用 服務(wù)中的方法,解除綁定后,服務(wù)確實已經(jīng)調(diào)用了 onDestory方法銷毀了服務(wù),為什么還能調(diào)用服務(wù)中的方法呢??? ?? 通常情況下 bindService()這個方法是怎么用的呢?活動的代碼:package?com.example.myservicetest; ? import?com.example.myservicetest.MyService.*; ? import?android.app.*; import?android.content.*; import?android.view.*; import?android.view.View.*; import?android.os.*; import?android.util.Log; import?android.widget.*; ? public?class?MainActivity?extends?Activity?implements?OnClickListener?{ ? ??//-------------------------------------------------------------------- ??private?Button?b1,?b2,?b3,?b4,?b5; ??private?ServiceConnection?connection; ??private?MyService?myService; ??private?Intent?in; ??? ??//-------------------------------------------------------------------- ??@Override ??protected?void?onCreate(Bundle?savedInstanceState) ??{ ????super.onCreate(savedInstanceState); ????setContentView(R.layout.activity_main); ????? ????//獲取按鈕 ????b1?=?(Button)findViewById(R.id.bind); ????b2?=?(Button)findViewById(R.id.unbind); ????b3?=?(Button)findViewById(R.id.play); ????b4?=?(Button)findViewById(R.id.start); ????b5?=?(Button)findViewById(R.id.stop); ????? ????//注冊點(diǎn)擊事件 ????b1.setOnClickListener(this); ????b2.setOnClickListener(this); ????b3.setOnClickListener(this); ????b4.setOnClickListener(this); ????b5.setOnClickListener(this); ????? ????//用于連接服務(wù) ????connection?=?new?ServiceConnection() ????{ ??????@Override ??????public?void?onServiceDisconnected(ComponentName?name) ??????{ ????????Log.e("connection",?"連接意外丟失"); ??????} ??????? ??????@Override ??????public?void?onServiceConnected(ComponentName?name,?IBinder?service) ??????{ ????????myService?=?((MyBinder)service).getMyService(); ????????Log.e("connection",?"連接完成"); ??????} ????}; ????? ????//啟動服務(wù)意圖 ????in?=?new?Intent(this,?MyService.class); ????? ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?void?onClick(View?v) ??{ ????switch(v.getId()) ????{ ????//啟動服務(wù) ????case?R.id.start: ??????startService(in); ??????break; ??????? ????//停止服務(wù) ????case?R.id.stop: ??????stopService(in); ??????break; ??????? ????//綁定服務(wù) ????case?R.id.bind: ??????bindService(new?Intent(this,?MyService.class),?connection,?BIND_AUTO_CREATE); ??????break; ??????? ????//解綁服務(wù) ????case?R.id.unbind: ??????unbindService(connection); ??????break; ??????? ????//調(diào)用服務(wù)中的方法 ????case?R.id.play: ??????myService.play(); ??????break; ????} ??} ? }服務(wù)的代碼:package?com.example.myservicetest; ? import?android.app.*; import?android.content.*; import?android.os.*; import?android.util.Log; ? public?class?MyService?extends?Service?{ ? ??private?MyBinder?mBinder; ? ??public?class?MyBinder?extends?Binder?{ ????public?MyService?getMyService() ????{ ??????return?MyService.this; ????} ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?void?onCreate() ??{ ????super.onCreate(); ????mBinder?=?new?MyBinder(); ????Log.e("Service",?"創(chuàng)建服務(wù)"); ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?IBinder?onBind(Intent?intent) ??{ ????Log.e("Service",?"執(zhí)行onBind"); ????return?mBinder; ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?int?onStartCommand(Intent?intent,?int?flags,?int?startId) ??{ ????Log.e("Service",?"執(zhí)行onStartCommand"); ????return?START_NOT_STICKY; ??} ??? ??//-------------------------------------------------------------------- ??@Override ??public?boolean?onUnbind(Intent?intent) ??{ ????Log.e("Service",?"執(zhí)行onUnbind"); ????return?false; ??} ??? ??//-------------------------------------------------------------------- ??@Override ??public?void?onDestroy() ??{ ????Log.e("Service",?"服務(wù)銷毀"); ????super.onDestroy(); ??} ??? ??? ??? ??? ??//-------------------------------------------------------------------- ??public?void?play() ??{ ????Log.e("binder",?"播放音樂"); ??} ? }
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關(guān)注
  • 2844 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號