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

Android 码拜 8年前 (2016-05-02) 1810次浏览
    为什么本人用 unbindService 方法解除了绑定后,仍然可以在 活动中调用 服务中的方法,解除绑定后,服务确实已经调用了 onDestory方法销毁了服务,为什么还能调用服务中的方法呢?
活动的代码:

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", "播放音乐");
  }
}

执行结果:
启动服务-绑定服务-播放-播放-解绑服务-停止服务-播放-播放
为什么用 unbindService() 方法解绑定服务后 仍然可以调用服务中的方法

解决方案

10

如上所说。你直接创建了myService对象,调用它的play方法,你的对象在,能调用它的方法,这是最正常不过的。

10

service的onstart中调用本人的play方法。播放调用startservice(),停止播放调用stopservice

30

play方法在,不代表service在运行,只是说明这个类没有被回收

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明为什么用 unbindService() 方法解绑定服务后 仍然可以调用服务中的方法
喜欢 (0)
[1034331897@qq.com]
分享 (0)