• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

为什么用 unbindService() 方法解绑定服务后 仍然可以调用服务中的方法?

为什么我用 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", "播放音乐");  } }

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

发新帖

发布任务需求已有1031166位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!