mui.plusReady(function(){
testThread()
})
function testThread() {
var App = plus.android.runtimeMainActivity();
var Thread = plus.ios.importClass('java.lang.Thread');
var SocketChannel = plus.android.importClass('java.nio.channels.SocketChannel');
var Selector = plus.android.importClass('java.nio.channels.Selector');
var SelectionKey = plus.android.importClass('java.nio.channels.SelectionKey');
var InetSocketAddress = plus.android.importClass('java.net.InetSocketAddress');
var SocketAddress = plus.android.importClass('java.net.SocketAddress');
var ByteBuffer = plus.android.importClass('java.nio.ByteBuffer');
var Iterator = plus.android.importClass('java.util.Iterator');
var method = plus.android.importClass('java.lang.reflect.Method');
var OutputStreamWriter = plus.android.importClass('java.io.OutputStreamWriter');
//测试改良
var StrictMode = plus.android.importClass('android.os.StrictMode');
var Build = plus.android.importClass('android.os.Build');
if(Build.VERSION.SDK_INT > 9) {
var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
selectionKey = new SelectionKey();
var Runnable = plus.android.implements('java.lang.Runnable', {
"run": function() {
//选择一组键,其相应的通道已为 I/O 操作准备就绪。
//此方法执行处于阻塞模式的选择操作。
//返回此选择器的已选择键集。
var DatagramPacket = plus.android.importClass("java.net.DatagramPacket");
var DatagramSocket = plus.android.importClass("java.net.DatagramSocket");
var InetAddress = plus.android.importClass("java.net.InetAddress");
var intent = plus.android.newObject("android.content.Intent");
console.log(intent)
// InetAddress
var addr = "192.168.2.230",
port = 8080, // 60000
TTLTime = 5000;
var ia = new InetAddress();
var address = ia.getByName(addr);
console.log(address);
// socket
var socket = new DatagramSocket();
socket.setSoTimeout(TTLTime);
var data = [104, 101, 108, 108, 111];
var packet = new DatagramPacket(data, data.length, address, port);
// 发送数据
try {
socket.send(packet);
} catch(e) {
console.log(e);
}
console.log("Invoked Object’s wahaha: ");
//selectionKeys.clear();
}
});
//方式1启动线程
App.runOnUiThread(Runnable);
}
你可以试下,我试过可以发送UDP给后台,后台能接收到信息
6 个评论
要回复文章请先登录或注册
1***@qq.com
w***@126.com
梦之风舞者
1***@qq.com
h***@163.com
张得愁