为什么我用 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 关注
- 2800 浏览
添加回答
举报
0/150
提交
取消