繁体   English   中英

Android中的网络设备清单

[英]Listing of the Network Devices in Android

在这里已回答类似或相同的问题

我正在创建一个Android应用程序,该应用程序将广播消息发送到网络,并准备响应的设备列表。

现在我做了什么:

我创建了一个ActivityDeviceManagerWindow.java ,它调用了一个线程Sender.java Sender.java负责发送广播消息。

然后, DeviceManagerWindow.java调用另一个线程,该线程负责侦听响应的设备。 devices回复后,将在活动中列出回复的设备。 为此,我有一个名为deviceListTableLayout

我写了什么代码:

DeviceManagerWindow.java This method is called when a button for search is pressed

public void searchDevice(View v) throws IOException, InterruptedException
{
    //Log.v("TableLayout:",view.toString());
    sendMulticastFlyport = new Thread(new FlyportSender(MAC));
    sendMulticastFlyport.start();
    new Thread()
    {
        public void run()
        {
            MulticastSocket socketComputer=null;
            try
            {
                socketComputer = new MulticastSocket(WifiConstants.COMPUTER_RECV_PORT);
                socketComputer.joinGroup(InetAddress.getByName(WifiConstants.COMPUTER_NETWORK_ADDR));
                socketComputer.setSoTimeout(1*60*1000);
                byte[] inBufComputer = new byte[1024];
                DatagramPacket inPacketComputer = new DatagramPacket(inBufComputer, inBufComputer.length);

                while(true)
                {
                    System.out.println("Listening...");
                    socketComputer.receive(inPacketComputer);
                    System.out.println("Received");
                    String msg = new String(inBufComputer, 0, inPacketComputer.getLength());
                    DeviceInformation device = new DeviceInformation(1, msg, inPacketComputer.getAddress().toString());

                    addDevice(device, false, 1);

                    Log.v("Received:","Received Computer From :" + inPacketComputer.getAddress() + " Msg : " + msg);
                    //System.out.write(inPacket.getData(),0,inPacket.getLength());
                    System.out.println();
                    Thread.sleep(2000);
                }
            }
            catch(Exception e)
            {
                Log.v("Exception:","During Receiving Computer: "+e.toString());
                try
                {
                    addDevice(null, true, 1);
                }
                catch (IOException e1)
                {
                    Log.v("Exception:", "Computer End Error: " +e1);
                }
            }
            finally
            {
                socketComputer.close();
            }
        }
    }.start();

以下代码创建一个列表:

public void addDevice(DeviceInformation device, boolean bool, int type) throws IOException
{
    TableLayout tb = (TableLayout) findViewById(R.id.DeviceList);
    Log.v("addDevice","Called");
    if(bool)
    {
        LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        TableRow tr = new TableRow(getApplicationContext());
        TextView tv = new TextView(getApplicationContext());
        System.out.println(tb);
        tv.setLayoutParams(layout);
        tr.setLayoutParams(layout);
        String message;
        Log.v("addDevice","Device Timeout");
        switch(type)
        {
            case 1:
                computerEnd=true;
                break;
            case 2:
                raspberryEnd=true;
                break;
            case 3:
                flyportEnd=true;
                break;
        }
        if(computerEnd && raspberryEnd && flyportEnd)
        {
            if(rowCounter>0)
            {
                message = "No More Devices";
            }
            else
            {
                message = "No Devices Found"; 
            }
            tv.setText(message);
            tv.setTextColor(Color.WHITE);
            if(rowCounter%2==0)
            {
                tr.setBackgroundColor(Color.DKGRAY);
            }
            else
            {
                tr.setBackgroundColor(Color.GRAY);
            }
            tv.setVisibility(1);
            tr.addView(tv);
            tb.addView(tr);
        }
    }
    else
    {   
        LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        TableRow tr = new TableRow(getApplicationContext());
        TextView tv = new TextView(getApplicationContext());

        tv.setLayoutParams(layout);
        tr.setLayoutParams(layout);

        Log.v("addDevice","Received");
        String textToDisplay = device.getDeviceTypeString()+"\n"+device.getIPAddress(); //Write the text to display
        tv.setText(textToDisplay);
        tv.setTextColor(Color.WHITE);
        Drawable img;
        if(device.getDeviceType()==1)
        {
            img = getApplicationContext().getResources().getDrawable(R.drawable.pc);
        }
        else if(device.getDeviceType()==2)
        {
            img = getApplicationContext().getResources().getDrawable(R.drawable.raspberry);
        }
        else
        {
            img = getApplicationContext().getResources().getDrawable(R.drawable.flyport);
        }
        img.setBounds(0,0,70,45);
        tv.setCompoundDrawables(null, null, img, null);
        tv.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

            }
        });
        if(rowCounter%2==0)
        {
            tr.setBackgroundColor(Color.DKGRAY);
        }
        else
        {
            tr.setBackgroundColor(Color.GRAY);
        }
        rowCounter++;
        Log.v("Result","Device Added");
    }
}

现在,它向我显示了logCat中的错误,如下所示:

05-11 22:01:10.165: E/AndroidRuntime(13873): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

我从中得出的结论是,仅允许UIThread访问创建的视图。

以前我曾尝试过:

new Thread()
{
    public void run()
    {
        runOnUiThread(){
        MulticastSocket socketComputer=null;
        try
        {
            ....        
            ....
            ....
    }
}

那时我收到一个错误:

主线程无法访问网络

在此之前,我曾尝试使用从Receiving.java线程文件中调用的synchronized方法。 但是它也给出了不创建列表的错误。

我尝试了所有可能的方法。

现在我该怎么办?

您发现正确。 现在,您可以学习使用处理程序将信息传递到UI线程(请参阅http://www.techotopia.com/index.php/A_Basic_Overview_of_Android_Threads_and_Thread_handlers )或AsyncTask(请参见http://developer.android.com/reference/ android / os / AsyncTask.html )。

我个人更喜欢AsyncTask。 您可以将执行搜索的代码粘贴到doInBackground()方法中(不需要使用单独的线程,doInBackground()已经为您完成此操作),并将与UI相关的代码(列表创建代码)粘贴到onPostExecute( ) 方法。 如果尚未从链接中充分了解AsyncTask的工作方式,请搜索更多示例。

编辑:如果您希望设备搜索代码无限期运行,则必须诉诸处理程序,因为AsyncTask希望doInBackground()方法在运行onPostExecute()之前完成。 查看哪个选项更适合您的需求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM