Android 仅等待输入X时间
我正在尝试从输入流读取数据,但如果程序在X个时间段内未接收数据,我希望终止尝试并返回Android 仅等待输入X时间,android,io,wait,Android,Io,Wait,我正在尝试从输入流读取数据,但如果程序在X个时间段内未接收数据,我希望终止尝试并返回-1。我以前使用的是Thread.sleep(X),但后来意识到这是一种完全不正确的方法。如果有人有任何想法,请告诉我。这是我从输入流读取的代码 try { // Read from the InputStream bytes = mmInStream.read(buffer, 0, length);
-1
。我以前使用的是Thread.sleep(X)
,但后来意识到这是一种完全不正确的方法。如果有人有任何想法,请告诉我。这是我从输入流读取的代码
try {
// Read from the InputStream
bytes = mmInStream.read(buffer, 0, length);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
//break;
}
您可以启动一个新线程,并在其中等待x时间。传递对活动的引用,一旦时间结束,就可以从时间线程调用活动中的方法 例如
我希望这有帮助 您可以启动一个新线程并在其中等待x个时间量。传递对活动的引用,一旦时间结束,就可以从时间线程调用活动中的方法 例如 我希望这有帮助 您可以使用来执行此操作 首先,您需要一个将作为“未来”值返回的类: 然后您需要使用executor服务,并使用如下方式:
ExecutorService service = Executors.newSingleThreadExecutor();
Future<ReadResult> future = service.submit(new Callable<ReadResult>() {
@Override
public ReadResult call() throws Exception {
bytes = mInStream.read(buffer, 0, length);
return new ReadResult(bytes, buffer);
}
});
ReadResult result = null;
try {
result = future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
// Thread was interrupted
e1.printStackTrace();
} catch (ExecutionException e1) {
// Something bad happened during reading
e1.printStackTrace();
} catch (TimeoutException e1) {
// read timeout
e1.printStackTrace();
}
if (result != null) {
// here you can use it
}
ExecutorService service=Executors.newSingleThreadExecutor();
Future=service.submit(new Callable()){
@凌驾
public ReadResult调用()引发异常{
字节=mInStream.read(缓冲区,0,长度);
返回新的读取结果(字节、缓冲区);
}
});
ReadResult=null;
试一试{
结果=future.get(10,时间单位:秒);
}捕捉(中断异常e1){
//线程被中断
e1.printStackTrace();
}捕获(执行异常e1){
//在阅读过程中发生了一些不好的事情
e1.printStackTrace();
}捕获(超时异常e1){
//读取超时
e1.printStackTrace();
}
如果(结果!=null){
//在这里你可以使用它
}
这样你就能实现你的目标。请注意,最好将可调用类的子类化,该类将接受inputstream作为构造函数参数,然后使用类变量。您可以使用
首先,您需要一个将作为“未来”值返回的类:
然后您需要使用executor服务,并使用如下方式:
ExecutorService service = Executors.newSingleThreadExecutor();
Future<ReadResult> future = service.submit(new Callable<ReadResult>() {
@Override
public ReadResult call() throws Exception {
bytes = mInStream.read(buffer, 0, length);
return new ReadResult(bytes, buffer);
}
});
ReadResult result = null;
try {
result = future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
// Thread was interrupted
e1.printStackTrace();
} catch (ExecutionException e1) {
// Something bad happened during reading
e1.printStackTrace();
} catch (TimeoutException e1) {
// read timeout
e1.printStackTrace();
}
if (result != null) {
// here you can use it
}
ExecutorService service=Executors.newSingleThreadExecutor();
Future=service.submit(new Callable()){
@凌驾
public ReadResult调用()引发异常{
字节=mInStream.read(缓冲区,0,长度);
返回新的读取结果(字节、缓冲区);
}
});
ReadResult=null;
试一试{
结果=future.get(10,时间单位:秒);
}捕捉(中断异常e1){
//线程被中断
e1.printStackTrace();
}捕获(执行异常e1){
//在阅读过程中发生了一些不好的事情
e1.printStackTrace();
}捕获(超时异常e1){
//读取超时
e1.printStackTrace();
}
如果(结果!=null){
//在这里你可以使用它
}
这样你就能实现你的目标。请注意,最好将可调用类的子类化,该类将接受inputstream作为构造函数参数,然后使用类变量。我不确定这是否有效,它似乎类似于使用
Thread.sleep()
。我想调用mmInStream.read()
,如果X时间量
在InStream
获得一个字节之前经过,我想返回一个-1
的值。我不确定这是否有效,它似乎类似于使用线程.sleep()
。我想调用mmInStream.read()
,如果X时间量
在InStream
获得一个字节之前经过,我想返回-1
的值。
ExecutorService service = Executors.newSingleThreadExecutor();
Future<ReadResult> future = service.submit(new Callable<ReadResult>() {
@Override
public ReadResult call() throws Exception {
bytes = mInStream.read(buffer, 0, length);
return new ReadResult(bytes, buffer);
}
});
ReadResult result = null;
try {
result = future.get(10, TimeUnit.SECONDS);
} catch (InterruptedException e1) {
// Thread was interrupted
e1.printStackTrace();
} catch (ExecutionException e1) {
// Something bad happened during reading
e1.printStackTrace();
} catch (TimeoutException e1) {
// read timeout
e1.printStackTrace();
}
if (result != null) {
// here you can use it
}