UDP发送关于Android4.0的版本和之前 [英] UDP sending on Android4.0 and before versions
问题描述
在我的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());
}
}
似乎没有错误和异常,而它JAVA Application.While正常工作,它只是不会在我的手机I9300工作。我改变在AndroidManifest.xml code code从
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" />
要code &LT;采用-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =8/&GT;
它只是做工精细!我简直不敢understarnd为什么这个一次发生?
任何人都可以帮我吗?
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
使用许可权的android:NAME =android.permission.INTERNET对
"uses-permission android:name="android.permission.INTERNET"
然后SDK8后,你必须使用一个线程像这样
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 ! :-(
这篇关于UDP发送关于Android4.0的版本和之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!