简体   繁体   English

如何等到服务绑定?

[英]How to wait until service is bound?

I'm programing a Bluetooth Service for my application.我正在为我的应用程序编写蓝牙服务。 I don't know a lot about services in android but I managed to start one and use it the way I want.我对 android 中的服务了解不多,但我设法启动了一个服务并按照我想要的方式使用它。

My problem comes just on the very start when I try to use a function from the service on the onCreate() method from Activity (It doesnt have enought time to bind the service).当我尝试在 Activity 的onCreate()方法上使用服务中的函数时,我的问题就出现在刚开始时(它没有足够的时间来绑定服务)。

I structurated the code into 3 classes:我将代码结构化为 3 个类:

  • BluetoothService: Class that implements the service BluetoothService:实现服务的类

    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 }
  • BluetoothActivity: Abstract class (for reusability) that handles bluetooth service connection. BluetoothActivity:处理蓝牙服务连接的抽象类(用于可重用性)。

     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 }
  • ScanActivity: Activity class that extends from BluetoothActivity and use BluetoothService methods on 'onCreate()'. ScanActivity:从 BluetoothActivity 扩展的活动类,并在“onCreate()”上使用 BluetoothService 方法。

Variable 'isBound' is intended to be use in order to know if the service is bound (or if we have to wait).变量“isBound”旨在用于了解服务是否已绑定(或者我们是否必须等待)。

What I tried:我试过的:

  • Use 'wait()' and 'notify()' but didn't worked for me.使用 'wait()' 和 'notify()' 但对我不起作用。
  • Use 'while(!isBound)' to wait but the application just crash.使用 'while(!isBound)' 等待但应用程序崩溃。
  • Use handlers to wait x miliseconds.使用处理程序等待 x 毫秒。 This worked but I know is not the way since the service may not be bound.这有效,但我知道不是这样,因为服务可能不受约束。

So my question is how can I wait on the 'onCreate()' method until 'isBound' gets true?所以我的问题是如何等待 'onCreate()' 方法直到 'isBound' 为真?

I just realized I can create two functions on BluetoothService and call them on onServiceConnected() and onServiceDisconnected() :我刚刚意识到我可以在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;
}

And then just override them on the extended class:然后在扩展类上覆盖它们:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM