如何从另一个类调用内部类的方法?

时间:2013-12-01 15:46:00

标签: android class static nested

public class MusicService extends Service  implements MediaPlayer.OnErrorListener{


private final IBinder mBinder = new ServiceBinder();
MediaPlayer mPlayer;
private int length = 0;

public MusicService() { }

public class ServiceBinder extends Binder {
     MusicService getService()
     {
        return MusicService.this;
     }
}

我想从anothere类调用getService方法,如下所示

public class SplashScreen extends Activity {

// splash screen timer
private static int SPLASH_TIME_OUT= 3000;


// for music player in background 

private boolean mIsBound = false;
private MusicService mServ;
private ServiceConnection Scon =new ServiceConnection(){

    public void onServiceConnected(ComponentName name, IBinder
     binder) {

        mServ = ((MusicService.ServiceBinderbinder).getService();


    }

    public void onServiceDisconnected(ComponentName name) {
        mServ = null;
    }



    };

它显示行mServ中的语法错误=((MusicService.ServiceBinderbinder).getService();

2 个答案:

答案 0 :(得分:3)

使用

    mServ = ((MusicService.ServiceBinderbinder)binder).getService();

而不是

    mServ = ((MusicService.ServiceBinderbinder).getService();

从onServiceConnected

初始化MusicService mServ

答案 1 :(得分:1)

你缺少'活页夹'' 变化:

mServ = ((MusicService.ServiceBinderbinder).getService();

有关:

mServ = ((MusicService.ServiceBinderbinder)binder).getService();