【问题标题】:How to bind multiple different services for android?如何为android绑定多个不同的服务?
【发布时间】:2019-06-03 21:26:45
【问题描述】:

我想在活动开始时启动 2 个服务,但只有第一个启动,第二个在 bindService() 失败。只有当我想对它给我一个空指针的服务做某事时,没有错误。我也试图等待做某事,但服务永远不会启动。

这 2 个服务非常相似,我只是想知道实施有什么问题。我尝试调试,bindservice() 函数在 startSoundmanagerService 返回 0,可能是问题的根源,但我不知道为什么。

public class SimulationActivity extends AppCompatActivity {

    BluetoothService BService;
    boolean mBound = false;
    SoundManager SService;
    boolean sBound = false;

    @Override
    protected void onStart() {
        super.onStart();
        if (bluetoothAdapter.isEnabled()) {
            setStatusText("Bluetooth on");
        }
        else {
            setStatusText("Bluetooth off");
        }
        if(!mBound) {
            startServer();
        }
        conStatImageView.setImageResource(R.drawable.connection_off);
        if(!sBound){
            startSoundManagerService();
        }

    @Override
    protected void onStop() {
        super.onStop();
        if(mBound) {
            unbindService(bConnection);
            mBound = false;
        }
        if(sBound){
            unbindService(sConnection);
            sBound = false;
        }


    }

    private ServiceConnection bConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                                        IBinder service) {
                BluetoothService.LocalBinder binder = 
                (BluetoothService.LocalBinder) service;

                BService = binder.getService();
                mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
        

    private ServiceConnection sConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
                                    IBinder service) {
        
            SoundManager.LocalBinder binder = (SoundManager.LocalBinder) service;
            SService = binder.getService();
            sBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        sBound = false;
    }


    public void startServer(){

        if (bluetoothAdapter == null) {
            Log.d("tag","Device doesn't support Bluetooth") ;
        }

        if (!bluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
        if(bluetoothAdapter.isEnabled()){
            startBluetoothservice();
        }

    }

    private void startBluetoothservice(){

        Intent intent = new Intent (this,BluetoothService.class);
        bindService(intent, bConnection, Context.BIND_AUTO_CREATE);
        Log.i(TAG,"Trying to start bluetoothservice");
    }
    private void startSoundManagerService(){

        Intent intent = new Intent (this,SoundManager.class);
        bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
        Log.i(TAG,"Trying to start soundservice");

    }

那么我怎样才能在 1 个活动中实现 2 个不同的服务呢?

编辑解决方案:我忘记在清单文件中注册服务。 ;)

【问题讨论】:

    标签: java android service android-service


    【解决方案1】:

    我找到了解决方案:我忘记在清单中注册第二个服务。就是这样;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多