【问题标题】:Android thread doesn't wait just runs onceAndroid线程不等待只运行一次
【发布时间】:2015-09-15 13:34:33
【问题描述】:

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

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。

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

【问题讨论】:

  • 您发布的代码中没有线程
  • 注意:Android 服务在“主”线程上运行,除非您(开发人员)对其进行编码以启动或使用……它是什么? IntentService - 我相信。
  • 来自Android Developer :“IntentService 执行以下操作: •创建一个默认工作线程,该线程执行交付给 onStartCommand() 的所有意图,与应用程序的主线程分开。 •创建一个传递一个工作队列你的 onHandleIntent() 实现的意图一次,所以你永远不必担心多线程。 •在处理完所有启动请求后停止服务,所以你永远不必调用 stopSelf()。 •..."
  • @Fildor 你能举一个更准确地应用于我的情况的例子吗?

标签: java android multithreading usb


【解决方案1】:

您需要调用处理程序,例如 new Thread(handler).start();而不是 handler.run();

因为运行,在同一个线程中执行,所以你不会得到等待/通知

【讨论】:

  • Thread t = new Thread(handler); t.start(); 给了我这个错误。 AndroidRuntime(2874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2014-03-04
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2012-07-13
相关资源
最近更新 更多