1
我是android編程的初學者。我嘗試使用下面的代碼播出Wi-Fi直連的消息:用android直播在android中廣播
公共類FileTransferService擴展IntentService {
public static final String host= "255.255.255.255";
InetAddress broadcastAddress = InetAddress.getByName(host);// Exception: Unknown host exception
int port = 8888;
protected void onHandleIntent(Intent intent) {
Log.d(WiFiDirectActivity.TAG,"m in 1");
Context context = getApplicationContext();
DatagramSocket socket;
try {
socket = new DatagramSocket(port);
socket.setBroadcast(true);
socket.connect(broadcastAddress, port);
String message = "Hello";
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(
buffer, buffer.length, broadcastAddress, port);
socket.send(packet); // <----- Causes a SocketException
} catch (IOException e) {
Log.d(WiFiDirectActivity.TAG, e.getMessage(), e);
}
}
}
它顯示我的GetByName方法()方法未知的主機異常。反正有替代方法嗎?我正走在一條正確的道路上嗎?我是否需要添加任何內容以發送消息。
在此先感謝
原諒我@ user2645907但Android的API正在使用? Android 4.x?或以下? – gumuruh