我有一个奇怪的问题,我不知道如何解决它。 我想从我正在运行的服务获得连接。 我用方法创建这个内部类:
public class PlayMusicServiceBinder extends Binder{
public PlayMusicService getService(){
return PlayMusicService.this;
}
}
然后我开始服务并与它建立连接:
private PlayMusicService pms
startService(playServiceIntent.putExtra("songs", songs).putExtra("currentPosition", currentPosition).putExtra("path", path));
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
pms = ((PlayMusicService.PlayMusicServiceBinder)service).getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
pms = null;
}
};
onBind();
onBind():
public void onBind(){
this.bindService(new Intent(this, PlayMusicService.class),serviceConnection, Context.BIND_AUTO_CREATE);
}
编译器在这一行pms = ((PlayMusicService.PlayMusicServiceBinder)service).getService();
上说:“无法投射”
我已经看到很多例子都是这样做的,没有人抱怨这些错误
请帮帮我。
感谢。
答案 0 :(得分:0)
您是否在PlayMusicService类中添加了此代码?
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
private final IBinder mBinder = new PlayMusicServiceBinder();