【问题标题】:How to wait until service is bound?如何等到服务绑定?
【发布时间】:2018-02-14 12:44:17
【问题描述】:

我正在为我的应用程序编写蓝牙服务。我对 android 中的服务了解不多,但我设法启动了一个并按照我想要的方式使用它。

当我尝试在 Activity 的 onCreate() 方法上使用服务中的函数时,我的问题刚开始(它没有足够的时间来绑定服务)。

我将代码分为 3 个类:

  • 蓝牙服务: 实现服务的类

    public class BluetoothService extends Service implements IScanService {
    
        private final IBinder bluetoothBinder = new BluetoothBinder();
    
        @Override
        public IBinder onBind(Intent intent) {
            return bluetoothBinder;
        }
    
        public class BluetoothBinder extends Binder {
            public BluetoothService getService() {
                return BluetoothService.this;
            }
        }
    
        // Other functions
    }
    
  • 蓝牙活动: 处理蓝牙服务连接的抽象类(可重用)。

    public abstract class BluetoothActivity extends AppCompatActivity {
    
        protected BluetoothService bluetoothService;
        protected volatile Boolean isBound = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Connecting to the service
            Intent intent = new Intent(this, BluetoothService.class);
            bindService(intent, connection, Context.BIND_AUTO_CREATE);
        }
    
        private ServiceConnection connection = new ServiceConnection() {
    
           @Override
            public void onServiceConnected(ComponentName className, IBinder service) {
               BluetoothService.BluetoothBinder binder = (BluetoothService.BluetoothBinder) service;
                bluetoothService = binder.getService();
                isBound = true;
            }
    
            @Override
            public void onServiceDisconnected(ComponentName className) {
                isBound = false;
            }
        };
    
        // Other functions
    }
    
  • 扫描活动: 从 BluetoothActivity 扩展并在“onCreate()”上使用 BluetoothService 方法的 Activity 类。

变量“isBound”旨在用于了解服务是否已绑定(或者我们是否必须等待)。

我尝试了什么:

  • 使用 'wait()' 和 'notify()' 但对我不起作用。
  • 使用 'while(!isBound)' 等待,但应用程序会崩溃。
  • 使用处理程序等待 x 毫秒。这行得通,但我知道不是这样,因为服务可能未绑定。

所以我的问题是如何等待 'onCreate()' 方法直到 'isBound' 变为真?

【问题讨论】:

  • 你不能。它会阻塞主线程,这会使应用程序崩溃(ANR)
  • 是的,我在尝试时注意到了,所以我在这里问。
  • 重点是什么?而你有onServiceConnected()
  • 我不能使用'onServiceConnected()',因为我想将代码分开来连接到服务,因为我将在不同的活动上使用它,这些活动将扩展'BluetoothActivity',这样他们就可以访问' bluetoothService' 方法。
  • @Zoe 我在哪里可以使用'wait()'?如有必要,我可以将代码移动到“onResume()”方法中。

标签: java android java-8 android-service android-service-binding


【解决方案1】:

我刚刚意识到我可以在BluetoothService 上创建两个函数并在onServiceConnected()onServiceDisconnected() 上调用它们:

private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        BluetoothActivity.this.onServiceConnected(name, (BluetoothService.BluetoothBinder) binder);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        BluetoothActivity.this.onServiceDisconnected(name);
    }
};

protected void onServiceConnected(ComponentName name, BluetoothService.BluetoothBinder binder){
    bluetoothService = binder.getService();
}

protected void onServiceDisconnected(ComponentName name){
    bluetoothService = null;
}

然后在扩展类上覆盖它们:

@Override
protected void onServiceConnected(ComponentName name, BluetoothService.BluetoothBinder binder) {
    super.onServiceConnected(name, binder);
    //TODO
}

@Override
protected void onServiceDisconnected(ComponentName name) {
    // TODO
    super.onServiceDisconnected(name);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    相关资源
    最近更新 更多