In my code
public void vodKeyCtrl(byte[] sendDataByte, int send_length) {
try {
Log.i(TAG, addr.toString());
InetAddress ia = InetAddress.getByName(addr.getKeyIp());
DatagramSocket socket = new DatagramSocket(0);
socket.connect(ia, addr.getKeyPort());
DatagramPacket dp = new DatagramPacket(sendDataByte, sendDataByte.length);
socket.send(dp);
socket.setSendBufferSize(sendDataByte.length);
socket.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
There seems no errors and exceptions,and it work fine on JAVA Application.While it just will not work on my phone i9300. I change code in AndroidManifest.xml code from
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
to code <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
It JUST WORK FINE!!!I just could not understarnd WHY THIS CASE HAPPENED??
Anyone can help me ?
Fisrt of all be sure your manifest have the internet permission
"uses-permission android:name="android.permission.INTERNET"
then after SDK8 you must use a thread like this
public class MainActivity extends Activity implements OnClickListener
{
public static final String DEST_IP = "192.168.1.7";
public static final int DEST_UDP_PORT = 1234;
public static final byte[] buf = {'H','e','l','l','o',' ','U','D','P'};
public Button Send_UDP_Button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Send_UDP_Button = (Button)findViewById(R.id.button1);
Send_UDP_Button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
new Thread(new Client()).start();
}
public class Client implements Runnable
{
@Override
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(DEST_IP);
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, DEST_UDP_PORT);
socket.send(packet);
socket.close();
}
catch (Exception e)
{
Log.w("Too Bad",e);
}
}
}
}
I hope this will help !
Sorry can't vote up your question I don't have enough reputation ! :-(