Android 绑定服务赢得';开始

Android 绑定服务赢得';开始,android,Android,我整天都在处理这个问题,我解决不了。我累坏了,不知道该怎么办。请帮忙。 我想创建一个在后台播放音乐的服务。但是接收的总是空的而不是服务。有时调用onServiceConnected,有时不调用 public class SoundService extends Service { private IBinder myBinder = new MyLocalBinder() ; private static boolean isSoundOn = false;

我整天都在处理这个问题,我解决不了。我累坏了,不知道该怎么办。请帮忙。 我想创建一个在后台播放音乐的服务。但是接收的总是空的而不是服务。有时调用onServiceConnected,有时不调用

 public class SoundService extends Service {
        private IBinder myBinder = new MyLocalBinder() ;
        private static boolean isSoundOn = false;
        private static boolean isBgMusicOn = false;
        private static int[] soundPoolIds = null;
        private Context appContext = null;
        private static SoundPlayer instance = null;
        private MediaPlayer mp = null;
        private SoundPool sndPool = null;   

public class MyLocalBinder extends Binder {
            public SoundService getService() {
                return SoundService.this;
            }
    }
        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return myBinder;
        }
        @Override
        public void onCreate() {
            super.onCreate();
        //  initSoundPools();

        }
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
        @Override
        public boolean onUnbind(Intent intent) {
            // TODO Auto-generated method stub
            return super.onUnbind(intent);
        }
        @Override
        public void onRebind(Intent intent) {
            // TODO Auto-generated method stub
            super.onRebind(intent);
        }
        public long getCurrentTime() {
            Time time = new Time();
            time.setToNow();

            return time.toMillis(false);

        }
        @SuppressLint("NewApi")
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {

            return super.onStartCommand(intent, flags, startId);
        } 
        private void initBackground() {
            //Some magic code here

        }
        private void playMusic(MediaPlayer mp) {
    //Some magic code here

        }
        private boolean musicIsPlaying(MediaPlayer mp)  {

    //Some magic code here

       }
             return false;
        }
        public void stopPlayingBackground() {
        //Some magic code here
        }
        private void stopPlaying(MediaPlayer mp,boolean release) {
            //Some magic code here
            }
        private int randInt(int min, int max) {

           //Some magic code here
        }
        private void initSoundPools() {
        //Some magic code here
        }
        public void turnSoundOn(boolean on) {
            isSoundOn = on;
        }
        public void turnMusicOn(boolean on) {
            isBgMusicOn = on;
        }
        public void playBackgroundMusic() {
        //Some magic code here
        }
        public void playSoundFx(int id) {
            //Some magic code here
        }
        }

    }
下面是一个类,它扩展了另一个类,而另一个类又扩展了活动

public class MainActivity extends LGame  {
    p
    private static final String TAG = "DEBUG";

    private static SoundService soundService;
    private static boolean isBound = false;
    private Thread serviceThread;
    private ServiceConnection myConnection;
    @Override
    public void onGamePaused() {


    }

    @Override
    public void onGameResumed() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onDestroy() {
        }
        if (soundService!=null) {
            soundService.stopSelf();
        }
        super.onDestroy();
    }
    public static SoundService getSoundService() {
        return soundService;
    }
    @Override
    public void onMain() {
        LTexture.ALL_LINEAR = true;
        LSetting setting = new LSetting();
        setting.width = 800;
        setting.height = 480;
        setting.fps = 30;
        setting.landscape = true;
        setting.showFPS = false;
        myConnection = new ServiceConnection() {

            public void onServiceConnected(ComponentName className,
                    IBinder service) {
                MyLocalBinder binder = (MyLocalBinder) service;
                soundService = binder.getService();
                isBound = true;
            } 

            public void onServiceDisconnected(ComponentName arg0) {
                isBound = false;
            }

           };
        serviceThread = new Thread(){
            public void run(){
                Intent intent = new Intent(getApplicationContext(), SoundService.class);
                getApplicationContext().bindService(intent, myConnection, Context.BIND_AUTO_CREATE);

            }
            };
            serviceThread.start();

        register(setting, MainGame.class);


    }
首先,我尝试在主线程中启动服务,但在这两种情况下都没有改变。 当然我已经在舱单上声明了服务。 我希望有人能帮我。 请提前向Thx寻求帮助。 编辑
我已经在你的帖子中提到过,我已经尝试过这样开始,但没有任何改变


救命!!我已经尝试了几乎所有的方法

您不应该在不同的线程中绑定服务,如果您想执行异步任务,您应该在服务本身内部创建线程

@Override
public void onMain() {
    LTexture.ALL_LINEAR = true;
    LSetting setting = new LSetting();
    setting.width = 800;
    setting.height = 480;
    setting.fps = 30;
    setting.landscape = true;
    setting.showFPS = false;
    myConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className,
                IBinder service) {
            MyLocalBinder binder = (MyLocalBinder) service;
            soundService = binder.getService();
            isBound = true;
        } 

        public void onServiceDisconnected(ComponentName arg0) {
            isBound = false;
        }

       };
    Intent intent = new Intent(getApplicationContext(), SoundService.class);
    getApplicationContext().bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    register(setting, MainGame.class);


}
来自Android文档:

bindService()方法立即返回,不带值


因此,在UI线程中绑定您的服务不会导致任何延迟或UI冻结。

对于将其更改为“声音服务”的编辑器-否。这是一个绑定服务,这就是为什么它有一个onBind和一个IBinder。我在你的帖子中提到过,我已经尝试过启动它,但没有任何改变