Garbage Collector가 여러 번 호출되어 루프 문에서 x 초 후에 publishProgress ()를 호출하는 방법을 찾고있었습니다.
이것이 내가 가진 것입니다.
protected Void doInBackground(Void... parms) {
Long size = source.length();
InputStream input = new FileInputStream(source);
OutputStream output = new FileOutputStream(destination);
// Transfer bytes from input to output
byte[] buf = new byte[1024];
int len;
long written = 0;
while ((len = input.read(buf)) > 0)
{
output.write(buf, 0, len);
written += 1024;
//This should be called after x seconds
publishProgress((int) (written * 100 / size));
}
input.close();
output.close();
}
찾았 ScheduledExecutorService
지만 while 루프 내부에서 구현하는 방법을 모르겠습니다.
이 솔루션은 AsyncTask를 사용하므로 UI 스레드를 차단하지 않습니다.
Long size = source.length();
InputStream input = new FileInputStream(source);
OutputStream output = new FileOutputStream(destination);
// Transfer bytes from input to output
byte[] buf = new byte[1024];
int len;
long written = 0;
while ((len = input.read(buf)) > 0) {
output.write(buf, 0, len);
written += 1024;
Long size = source.length();
InputStream input = new FileInputStream(source);
OutputStream output = new FileOutputStream(destination);
// Transfer bytes from input to output
byte[] buf = new byte[1024];
int len;
long written = 0;
while ((len = input.read(buf)) > 0) {
output.write(buf, 0, len);
written += 1024;
(new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... parms) {
mEventDataSource.deleteAll();
try {
Thread.currentThread().sleep(x * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// This should be called after x seconds
publishProgress((int) (written * 100 / size));
}
}).execute();
}
input.close();
output.close();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다