当我调用 bufferedReader.readLine() 时,我的 Android 应用程序冻结了;
My Android app is freezing when I call bufferedReader.readLine();
我为 Android phone 和 Android Wear 创建了一个新的 Android Studio 项目来测试 ChannelApi。在我的项目中似乎一切正常,除了在 onChannelOpened
方法中我无法在不冻结应用程序的情况下从 InputStream
读取。
这是我从可穿戴设备发送消息的方式(似乎有效):
Wearable.ChannelApi.openChannel(googleApiClient, node.getId(), channelIdentifier).setResultCallback(new ResultCallback<ChannelApi.OpenChannelResult>() {
@Override
public void onResult(ChannelApi.OpenChannelResult openChannelResult) {
final Channel channel = openChannelResult.getChannel();
channel.getOutputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetOutputStreamResult>() {
@Override
public void onResult(final Channel.GetOutputStreamResult getOutputStreamResult) {
final String message = "hello from your smartwatch";
try {
getOutputStreamResult.getOutputStream().write(message.getBytes());
Log.d(TAG, "sendMessageToNode: onResult: onResult: Message sent: " + message);
}
// ...
日志打印:D/MainActivity﹕ sendMessageToNode: onResult: onResult: Message sent: hello from your smartwatch
在聪明phone方面,我尝试按如下方式阅读消息:
@Override
public void onChannelOpened(final Channel channel) {
channel.getInputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetInputStreamResult>() {
@Override
public void onResult(Channel.GetInputStreamResult getInputStreamResult) {
Log.d(TAG, "onChannelOpened: onResult");
String message = "";
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = getInputStreamResult.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
//while ((line = bufferedReader.readLine()) != null) {
while (true) {
// Next line is freezing the app
line = bufferedReader.readLine();
Log.d(TAG, "onChannelOpened: onResult: line: " + line);
message += line + "\n";
if (line == null) break;
}
Log.d(TAG, "onChannelOpened: onResult: Received the following message: \" + message");
// ...
日志打印:D/MainActivity﹕ onChannelOpened: onResult
但仅此而已。
我启动了调试器,现在知道错误一定在这一行:line = bufferedReader.readLine();
。但是没有抛出异常或其他任何东西。调试器甚至告诉我缓冲区有正确的值:
我正在尝试发送 "hello from your smartwatch"。但是,该应用程序似乎冻结了。有谁知道为什么?
您需要在发送端关闭流,否则接收端将不知道是否有更多数据通过通道传输。
我为 Android phone 和 Android Wear 创建了一个新的 Android Studio 项目来测试 ChannelApi。在我的项目中似乎一切正常,除了在 onChannelOpened
方法中我无法在不冻结应用程序的情况下从 InputStream
读取。
这是我从可穿戴设备发送消息的方式(似乎有效):
Wearable.ChannelApi.openChannel(googleApiClient, node.getId(), channelIdentifier).setResultCallback(new ResultCallback<ChannelApi.OpenChannelResult>() {
@Override
public void onResult(ChannelApi.OpenChannelResult openChannelResult) {
final Channel channel = openChannelResult.getChannel();
channel.getOutputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetOutputStreamResult>() {
@Override
public void onResult(final Channel.GetOutputStreamResult getOutputStreamResult) {
final String message = "hello from your smartwatch";
try {
getOutputStreamResult.getOutputStream().write(message.getBytes());
Log.d(TAG, "sendMessageToNode: onResult: onResult: Message sent: " + message);
}
// ...
日志打印:D/MainActivity﹕ sendMessageToNode: onResult: onResult: Message sent: hello from your smartwatch
在聪明phone方面,我尝试按如下方式阅读消息:
@Override
public void onChannelOpened(final Channel channel) {
channel.getInputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetInputStreamResult>() {
@Override
public void onResult(Channel.GetInputStreamResult getInputStreamResult) {
Log.d(TAG, "onChannelOpened: onResult");
String message = "";
InputStream inputStream = null;
BufferedReader bufferedReader = null;
try {
inputStream = getInputStreamResult.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
//while ((line = bufferedReader.readLine()) != null) {
while (true) {
// Next line is freezing the app
line = bufferedReader.readLine();
Log.d(TAG, "onChannelOpened: onResult: line: " + line);
message += line + "\n";
if (line == null) break;
}
Log.d(TAG, "onChannelOpened: onResult: Received the following message: \" + message");
// ...
日志打印:D/MainActivity﹕ onChannelOpened: onResult
但仅此而已。
我启动了调试器,现在知道错误一定在这一行:line = bufferedReader.readLine();
。但是没有抛出异常或其他任何东西。调试器甚至告诉我缓冲区有正确的值:
我正在尝试发送 "hello from your smartwatch"。但是,该应用程序似乎冻结了。有谁知道为什么?
您需要在发送端关闭流,否则接收端将不知道是否有更多数据通过通道传输。