【发布时间】:2015-06-22 14:25:48
【问题描述】:
我有一个用于将文件上传到服务器的 Android 意图服务。这些文件将每 1 分钟添加到 intentservice 队列。它在操作系统小于 5.0.1 的 android 设备中运行良好。但是在 android 5.0.1 中上传文件时,如果网络丢失,我没有得到任何响应,没有抛出任何异常并且意图服务被阻止。从那时起文件没有上传。我必须重新启动应用程序才能使服务再次运行。下面是我在 onHandleIntent 方法中的代码。
int ReadBytes = 0;
int BUFSIZ = 4096;
byte Buf[] = new byte[BUFSIZ];
outputStream = conn.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
fileInputStream = new FileInputStream(source);
long prevBytesRead = 0;
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
while(ReadBytes>0){
dataOutputStream.write(Buf, 0, ReadBytes);
prevBytesRead += ReadBytes;
long totalbytes = fileInputStream.getChannel().size();
ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
}
outputStream.flush();
dataOutputStream.flush();
任何帮助将不胜感激。
【问题讨论】:
-
'long totalbytes = fileInputStream.getChannel().size();'。最好删除该声明。首先它不是真的。其次你不使用它。
标签: android android-intentservice