我该如何使用服务监听器

时间:2013-09-10 11:38:22

标签: android jmdns

我是韩国的Android开发者。我有一些问题,所以我进入这个网站。 我已经在网站上搜索了,但我没有解决问题。

    protected void onStart{
        if(android.os.Build.VERSION.SDK_INT > 9){
              StrictMode.ThreadPolicy.Builder builder = new Builder();
              StrictMode.ThreadPolicy policy = builder.permitAll().build(); 
              StrictMode.setThreadPolicy(policy);
        }

        WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);

        mMulticastLock = wifi.createMulticastLock("mDNSLock");
        mMulticastLock.setReferenceCounted(true);
        mMulticastLock.acquire();

        String ip = Formatter.formatIpAddress(wifi.getConnectionInfo().getIpAddress());
        mJmDNSManager = JmDNS.create(ip);
        //mJmDNSManager.addServiceListener(TYPE, mJmDNSServiceListener);        
        mJmDNSManager.addServiceTypeListener(mJmDNSServiceTypeListener);
   }

    private ServiceTypeListener mJmDNSServiceTypeListener = new ServiceTypeListener() {

        @Override
        public void subTypeForServiceTypeAdded(ServiceEvent event) {
              // TODO Auto-generated method stub
             Log.v(TAG, "== subTypeForServiceTypeAdded: "+ event.getType());
        }

        @Override
        public void serviceTypeAdded(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceTypeAdded: " + event.getType());
            mJmDNSManager.addServiceListener(event.getType(), mJmDNSServiceListener);
        }
    };

    protected ServiceListener mJmDNSServiceListener = new ServiceListener(){
        @Override
        public void serviceAdded(ServiceEvent event) {
            // TODO Auto-generated method stub
            Looper.prepare();
            Log.v(TAG, "== serviceAdded : " + event.getName());
            mServiceEvents = new ArrayList<ServiceEvent>();

            //mJmDNSManager.requestServiceInfo(event.getType(), event.getName(), 1);

            ServiceInfo info = event.getInfo();
                Toast.makeText(MainActivity.this, "onServiceAdded : " + info.getName() +                                                      
                "/" + info.getType() + "/" + info.getPort(), Toast.LENGTH_LONG).show();

            mServiceEvents.add(event);

            Log.v(TAG, "==mServiceEvents.size() : " + mServiceEvents.size());
            if(mServiceEvents.size() == 1) {
                   autoPairing();
                }
                else {
                     progressDialog.dismiss();

                 if (mDialog != null && !mDialog.isRemoving()) mDialog.dismiss();      
                        mDialog = new SelectDeviceDialog();
                 mDialog.show(getFragmentManager(), "SelectDeviceDialog");
                }
                Looper.loop();
        }

        @Override
        public void serviceRemoved(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceRemoved: " + event.getName());
        }

        @Override
        public void serviceResolved(ServiceEvent event) {
            // TODO Auto-generated method stub
            Log.v(TAG, "== serviceResolved: " + event.getInfo().getQualifiedName() + "      
                port:" + event.getInfo().getPort());
        }
    };

ServiceListener和ServiceTypeListener不起作用。所以我想知道解决问题。谢谢你的回复。

0 个答案:

没有答案