Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android线程不';别等了,就跑一次_Java_Android_Multithreading_Usb - Fatal编程技术网

Java Android线程不';别等了,就跑一次

Java Android线程不';别等了,就跑一次,java,android,multithreading,usb,Java,Android,Multithreading,Usb,我已经创建了一个广播服务,其中包含应该等待输入的可运行程序,它至少运行第一个可运行的内联程序一次,但不添加任何其他程序,也不等待和侦听 public class MainService extends Service { // Manager device; Emitter emitter; RFIDPhidget device; Service service; @Override public IBinder onBind(Intent in

我已经创建了一个广播服务,其中包含应该等待输入的可运行程序,它至少运行第一个可运行的内联程序一次,但不添加任何其他程序,也不等待和侦听

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();
    }
}
我曾经在一个活动中运行整个代码,并使用,
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();
        }
    }
对于try函数中发生的事情,所有侦听器都是相同的

我需要帮助,弄清楚如何让他们等待,或者至少在后台继续运行,这就是我转向服务的全部原因,这样我就不会依赖于UI


如果您需要任何进一步的信息,请不要害怕询问。

您需要像调用new-Thread(handler.start())一样调用处理程序;而不是handler.run()


因为run在同一个线程中执行,所以您不会得到wait/notify

您发布的代码中没有线程注意:Android服务在“主”线程上运行,除非您(开发人员)将其编码为启动一个线程或使用。。。那是什么?IntentService-我相信。From:“IntentService执行以下操作:•创建一个默认工作线程,该线程执行交付给onStartCommand()的所有意图,该线程与应用程序的主线程分离。•创建一个工作队列,一次将一个意图传递给OnHandleContent()实现,因此您永远不必担心多线程。•在处理完所有启动请求后停止服务,因此您永远不必调用stopSelf()。•…@Fildor您能给出一个更准确地应用于我的情况的示例吗?
Thread t=new Thread(handler);t、 start()给了我这个错误。AndroidRuntime(2874):java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序