我试图在我的 android 应用程序的单独线程中定义一个侦听器,但是,线程在此之前退出并且不等待调用回调函数。
下面是代码片段:
new Thread(new Runnable() {
public void run() {
ServiceConnection conn = new ServiceConnection() {
public void OnServiceConnected(ComponentName name, IBinder service) {
obj = <AIDL interface>.Stub.asInterface(service);
}
public void onServiceDisconnected(ComponentName name) {
obj = null;
}
Intent intent = new Intent("<service name>");
context.startService(intent);
boolean status = context.bindService(intent, conn, Context.BIND_AUTO_CREATE);
}).start();
现在,当我从主线程启动这个线程并等待 onServiceConnected() 发生时,它会永远等待并且永远不会被调用。我还检查了该线程的状态,它显示“已终止”。知道如何解决这个问题吗?