Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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/197.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 使用处理程序更新UI线程_Java_Android_Multithreading_User Interface_Handler - Fatal编程技术网

Java 使用处理程序更新UI线程

Java 使用处理程序更新UI线程,java,android,multithreading,user-interface,handler,Java,Android,Multithreading,User Interface,Handler,我已经在网上搜索了一些明确的例子,但是到目前为止还没有找到一个可以应用到我的项目中的 我正在尝试创建一个每100ms运行一次的工作线程。然后它应该用结果更新UI。经过一些研究,我决定我可能应该使用处理程序来管理UI更新。我找到了这个解决方案: 我的活动的处理程序: private final Handler handler = new Handler() { public void handleMessage(Message msg) { String aR

我已经在网上搜索了一些明确的例子,但是到目前为止还没有找到一个可以应用到我的项目中的

我正在尝试创建一个每100ms运行一次的工作线程。然后它应该用结果更新UI。经过一些研究,我决定我可能应该使用处理程序来管理UI更新。我找到了这个解决方案:

我的活动的处理程序:

private  final Handler handler = new Handler() {

        public void handleMessage(Message msg) {

        String aResponse = msg.getData().getString("message");

        if ((null != aResponse)) {

            // ALERT MESSAGE
            Log.v("udppacket", "UDP message!");
            if (msg.obj != null)
            {
                ManagerUdpPacket p = (ManagerUdpPacket) msg.obj;
                operatorListFragment.updateContent((int) p.getOperationTime());
            }
        }
        else
        {

        }    

    }
};
我的另一个类具有工作线程:

public class ManagerUdpReceiver 
{
    private int minPort = 1234;
    private int maxPort = 1240;
    private ArrayList<PortListener> portList;
    private Handler handler;
    private Thread portThread;
    private int queryInterval = 100;
    private boolean stop = false;


    public ManagerUdpReceiver(int minport, int maxport, Handler handler, int queryInterval)
    {
        minPort = minport;
        maxPort = maxport;
        this.handler = handler;
        this.queryInterval = queryInterval;

        //create port listeners from given range and start their threads


    start();
}

private void start()
{
    stop = false;
    // Create Inner Thread Class
    portThread = new Thread(new Runnable() 
    {
        // After call for background.start this run method call
        public void run() 
        {

            if (portList == null)
            {
                portList = new ArrayList<PortListener>();
                for (int i = minPort; i < maxPort; i++)
                {
                    portList.add(new PortListener(i));
                }
            }

            if (!stop)
            {
                ManagerUdpPacket p = portList.get(0).receive();
                threadMsg("moi", p);
                //mHandler.postDelayed(this, queryInterval);

            }
            else
            {
                //stop execution and close ports
                for (int i = 0; i < portList.size(); i++)
                {
                    portList.get(i).close();
                }
            }


        }

        //send message to the handler
        private void threadMsg(String msg, ManagerUdpPacket p) 
        {
             if (!msg.equals(null) && !msg.equals("")) 
             {
                 Message msgObj = handler.obtainMessage();
                 //msgObj.obj = p;
                 Bundle b = new Bundle();
                 b.putString("message", msg);
                 msgObj.setData(b);
                 handler.sendMessage(msgObj);
             }               
        }
    });

    // Start Thread
    portThread.start();
}



public void close()
    {   
        stop = true;
    }
}
公共类管理器驱动程序
{
私有int minPort=1234;
专用int maxPort=1240;
私有ArrayList端口列表;
私人经办人;
私有线程端口线程;
private int queryInterval=100;
私有布尔停止=false;
公共管理器预驱动程序(int minport、int maxport、Handler Handler、int queryInterval)
{
minPort=minPort;
maxPort=maxPort;
this.handler=handler;
this.queryInterval=queryInterval;
//从给定范围创建端口侦听器并启动其线程
start();
}
私有void start()
{
停止=错误;
//创建内线程类
portThread=新线程(新可运行()
{
//在后台调用之后。启动此运行方法调用
公开募捐
{
if(portList==null)
{
portList=newarraylist();
对于(int i=minPort;i
当我运行该程序时,我得到一个关于在UI线程中运行网络代码的异常。现在,工作线程应该接收并处理UDP数据包。但是,它的代码在portThread线程内部!我想handler.postDelayed(这是queryInterval);我使用它每100毫秒循环一次线程,不知何故,它会导致下一个循环在UI线程而不是我的工作线程中运行


所以我的问题是我做错了什么,以及如何修复它?或者,如何使循环在每100ms内正确工作?我也不确定处理程序应该放在哪里,因为我已经看到了它位于活动内部和工作线程内部的示例。

好的,我想我已经让它工作了,尽管我对它不满意,所以不检查它

基本上,我使用TimerTask每100毫秒运行一次代码,并通过处理程序通知UI线程。我不确定这是否是最好的选择(我听说计时器不是很好),但似乎很有效:

dataStreamTimer = new Timer();
    dataStreamTask = new TimerTask()
    {
        public void run() 
        {

            if (portList == null)
            {
                portList = new ArrayList<PortListener>();
                for (int i = minPort; i < maxPort; i++)
                {
                    portList.add(new PortListener(i));
                }
            }

            if (!stop)
            {

                ManagerUdpPacket p = portList.get(0).receive();
                threadMsg("moi", p);
                //handler.postDelayed(this, queryInterval);

                //stop thread until next query
                try {
                    synchronized(this){
                        this.wait(queryInterval);
                    }
                } catch (InterruptedException e) {
                    Log.e("ERR", "InterruptedException in TimerTask.run");
                }
            }
            else
            {
                //execution has been stopped, clear data:
                //stop execution and close ports
                for (int i = 0; i < portList.size(); i++)
                {
                    portList.get(i).close();
                }
            }

        }
dataStreamTimer=new Timer();
dataStreamTask=new TimerTask()
{
公开募捐
{
if(portList==null)
{
portList=newarraylist();
对于(int i=minPort;i
不太理解处理程序的用途。为什么不在后台线程上准备数据,然后使用
myActivity.runOnUIThread()
来运行updateContent()方法?可能是
p.getOperationTime()
被认为是网络操作,请尝试将此值保存到后台线程中的某个变量,然后通过UI线程将其发布。

您可能希望了解这一点。您还可以在每个周期后100毫秒在后台线程中以休眠方式运行代码。