android 绑定服务解除绑定服务还在吗,为什么用 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", "播放音乐");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值