绑定服务不会开始

时间:2014-08-03 18:52:00

标签: android

我整天都在处理这个问题,我无法解决它。我很累,不知道该怎么做。请帮忙。 我想创建在后台播放音乐的服务。但是接收始终为null而不是服务。有时调用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
        }
        }

    }

这是扩展另一个的类,它又扩展了Activity

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);


    }

首先,我尝试在主线程中启动服务,但两种情况都没有改变。 当然我已经在清单中声明了服务。 我希望有人可以帮助我。 请提前获得帮助。 的修改 我已经提到我已经尝试像你的帖子一样启动它但没有改变

帮助!!我已经尝试了几乎所有的东西

1 个答案:

答案 0 :(得分:0)

你永远不应该在不同的线程中绑定服务,如果你想做异步任务,你应该在服务本身内创建线程。

@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冻结。