亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

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

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

kokoromi 2016-07-04 20:44:57
?? 為什么我用 unbindService 方法解除了綁定后,仍然可以在 活動中調用 服務中的方法,解除綁定后,服務確實已經調用了 onDestory方法銷毀了服務,為什么還能調用服務中的方法呢??? ?? 通常情況下 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); ????? ????//注冊點擊事件 ????b1.setOnClickListener(this); ????b2.setOnClickListener(this); ????b3.setOnClickListener(this); ????b4.setOnClickListener(this); ????b5.setOnClickListener(this); ????? ????//用于連接服務 ????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",?"連接完成"); ??????} ????}; ????? ????//啟動服務意圖 ????in?=?new?Intent(this,?MyService.class); ????? ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?void?onClick(View?v) ??{ ????switch(v.getId()) ????{ ????//啟動服務 ????case?R.id.start: ??????startService(in); ??????break; ??????? ????//停止服務 ????case?R.id.stop: ??????stopService(in); ??????break; ??????? ????//綁定服務 ????case?R.id.bind: ??????bindService(new?Intent(this,?MyService.class),?connection,?BIND_AUTO_CREATE); ??????break; ??????? ????//解綁服務 ????case?R.id.unbind: ??????unbindService(connection); ??????break; ??????? ????//調用服務中的方法 ????case?R.id.play: ??????myService.play(); ??????break; ????} ??} ? }服務的代碼: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",?"創建服務"); ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?IBinder?onBind(Intent?intent) ??{ ????Log.e("Service",?"執行onBind"); ????return?mBinder; ??} ? ??//-------------------------------------------------------------------- ??@Override ??public?int?onStartCommand(Intent?intent,?int?flags,?int?startId) ??{ ????Log.e("Service",?"執行onStartCommand"); ????return?START_NOT_STICKY; ??} ??? ??//-------------------------------------------------------------------- ??@Override ??public?boolean?onUnbind(Intent?intent) ??{ ????Log.e("Service",?"執行onUnbind"); ????return?false; ??} ??? ??//-------------------------------------------------------------------- ??@Override ??public?void?onDestroy() ??{ ????Log.e("Service",?"服務銷毀"); ????super.onDestroy(); ??} ??? ??? ??? ??? ??//-------------------------------------------------------------------- ??public?void?play() ??{ ????Log.e("binder",?"播放音樂"); ??} ? }
查看完整描述

目前暫無任何回答

  • 0 回答
  • 0 關注
  • 2847 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號