我已經有一些難以讓JmDNS與我的Android AVD一起工作。我創建了3個應用程序。一個註冊ServiceListener並記錄任何活動的Android應用程序,一個與android應用程序執行相同操作的Java應用程序,以及另一個註冊服務的Java應用程序。 Java偵聽器應用程序將在另一個Java應用程序中啓動,但是android應用程序不會。我也試着同時運行兩個AVD,看看他們是否會相互接觸,但他們不會。我還應該提到我有權啓用INTERNET和CHANGE_WIFI_MULSTICAST_STATE。這裏是我的代碼:Android AVD無法注意到JmDNS服務
Android應用程序:
public class BonjourActivity extends Activity {
// Multicast
private WifiManager wifi;
private MulticastLock lock;
private JmDNS jmdns;
private String type = "_im._tcp.local.";
private ServiceListener listener;
private ServiceInfo serviceInfo;
// On Create
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Multicast
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("");
lock.setReferenceCounted(true);
lock.acquire();
// JmDNS
new AsyncTask<Object, Object, Object>(){
@Override
protected Object doInBackground(Object... params) {
// Create JmDNS
try {
jmdns = JmDNS.create();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Object result) {
// Add Listener
jmdns.addServiceListener(type, listener = new ServiceListener(){
@Override
public void serviceAdded(ServiceEvent ev) {
jmdns.requestServiceInfo(ev.getType(), ev.getName(), 1);
}
@Override
public void serviceRemoved(ServiceEvent ev) {
Log.d("Service", "Service Removed: " + ev.getName());
}
@Override
public void serviceResolved(ServiceEvent ev) {
Log.d("Service", "Service Resolved: " + ev.getInfo().getURL());
}
});
}
}.execute();
}
// On Destroy
public void onDestroy(){
// Release Lock
if (lock != null){
lock.release();
}
// Close JmDNS
if (jmdns != null){
jmdns.removeServiceListener(type, listener);
try {
jmdns.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.onDestroy();
}
}
的Java偵聽器應用程序:
public class Listener {
private static JmDNS jmdns;
private static String type = "_im._tcp.local.";
private static ServiceListener serviceListener;
private static ServiceInfo serviceInfo;
// Main
public static void main(String args[]){
try {
jmdns = JmDNS.create();
jmdns.addServiceListener(type, serviceListener = new ServiceListener(){
@Override
public void serviceAdded(ServiceEvent ev) {
System.out.println("Service Added: " + ev.getName());
jmdns.requestServiceInfo(ev.getType(), ev.getName(), 1);
}
@Override
public void serviceRemoved(ServiceEvent ev) {
System.out.println("Service Removed: " + ev.getName());
}
@Override
public void serviceResolved(ServiceEvent ev) {
System.out.println("Service Resolved: " + ev.getInfo().getURL());
}
});
System.out.println("Listener Added");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
的Java應用發件人:
public class Sender {
private static JmDNS jmdns;
private static String type = "_im._tcp.local.";
private static ServiceListener serviceListener;
private static ServiceInfo serviceInfo;
// Main
public static void main(String args[]){
try {
jmdns = JmDNS.create();
serviceInfo = ServiceInfo.create(type, "Test IM Service", 55555, "Instant messaging test service");
jmdns.registerService(serviceInfo);
System.out.println("Sender: Service Created");
new Timer().schedule(new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Closing..");
jmdns.unregisterAllServices();
try {
jmdns.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
}, 10000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
此外,logcat的給我一些錯誤,當我嘗試運行應用程序:
NetworkManagementSocketTagger setKernelCountSet(10009,0)失敗,錯誤號-2
WifiStateMachine錯誤!未處理的消息{what = 131157 when = -1ms}
可能有人知道爲什麼android應用程序無法獲取由其他應用程序生成的JmDNS服務?
將jmdns.addServiceListener(...將代碼塊從onPostExecute()移至doInBackground(),最好不要將您的偵聽器創建爲內聯匿名類。 – yorkw 2012-03-28 01:48:24