我创建了一个广播服务,其中包含应该等待输入的runnables,它至少运行第一次runnable内联,但不会添加任何其他的,也不会等待和监听。
public class MainService extends Service {
// Manager device;
Emitter emitter;
RFIDPhidget device;
Service service;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// For time consuming an long tasks you can launch a new thread here...
Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
}
/** Called when the activity is first created. */
@Override
public void onCreate() {
// LinearLayout lin_out = (LinearLayout)findViewById(R.id.lineout);
// lin_out.setVisibility(View.GONE);
Toast.makeText(this, " Service CREATED", Toast.LENGTH_LONG).show();
try
{
com.phidgets.usb.Manager.Initialize(this);
//device = new Manager();
device = new RFIDPhidget();
service = this;
emitter = new Emitter(this);
device.addAttachListener(new AttachListener() {
public void attached(final AttachEvent attachEvent) {
AttachEventHandler handler = new AttachEventHandler((RFIDPhidget)attachEvent.getSource(), service);
handler.run();
synchronized(handler) {
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
device.addDetachListener(new DetachListener() {
public void detached(final DetachEvent detachEvent) {
DetachEventHandler handler = new DetachEventHandler((RFIDPhidget)detachEvent.getSource(), service);
handler.run();
synchronized(handler) {
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
device.addTagGainListener(new TagGainListener() {
public void tagGained(TagGainEvent oe) {
TagGainedHandler handler = new TagGainedHandler(oe, emitter);
handler.run();
synchronized(handler)
{
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
device.addTagLossListener(new TagLossListener() {
public void tagLost(TagLossEvent oe) {
TagLossHandler handler = new TagLossHandler(oe, emitter);
handler.run();
synchronized(handler)
{
try {
handler.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
device.openAny();
} catch (PhidgetException pe) {
pe.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
device.close();
} catch (PhidgetException e) {
e.printStackTrace();
}
com.phidgets.usb.Manager.Uninitialize();
}
}
我曾经在一个活动中运行这整个代码,并为每个监听器变量'handler'使用runOnUIThread(handler);
但是因为这在服务中是不可能的,所以我开始使用handler.run();
。
它至少运行一次AttachEventHandler,它不会运行其他的,也不会等待它。以下是侦听器内部代码的示例。
public class AttachEventHandler implements Runnable {
RFIDPhidget device;
Service main_service;
public AttachEventHandler(RFIDPhidget device, Service main_service) {
this.device = device;
this.main_service = main_service;
}
public void run() {
try {
device.setAntennaOn(true);
device.setLEDOn(true);
Toast.makeText(main_service, "Hello " + device.getDeviceName() + ", Serial " + Integer.toString(device.getSerialNumber()), Toast.LENGTH_LONG);
} catch (PhidgetException e) {
e.printStackTrace();
}
// Notify that we're done
synchronized(this) {
this.notify();
}
}
所有听众都相同,接受他们的尝试功能。
我需要帮助弄清楚如何让他们等待或至少继续在后台运行,这就是我转移到服务的全部原因所以我不会依赖于UI。
如果您需要任何进一步的信息,请不要害怕。
答案 0 :(得分:0)
你需要调用你的处理程序,比如新的Thread(handler).start();而不是handler.run();
因为运行,在同一个线程中执行,所以你不会得到wait / notify