Java 如何指定在循环内x秒后调用publishProgress()?
由于垃圾收集器被多次调用,我正在寻找一种在循环语句中x秒后调用publishProgress()的方法 这就是我所拥有的:Java 如何指定在循环内x秒后调用publishProgress()?,java,android,Java,Android,由于垃圾收集器被多次调用,我正在寻找一种在循环语句中x秒后调用publishProgress()的方法 这就是我所拥有的: protected Void doInBackground(Void... parms) { Long size = source.length(); InputStream input = new FileInputStream(source); OutputStream output = new FileOutputStream(destin
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循环中实现它。将这一行放在循环中
handler.postDelayed(runnable, x);
在你的活动中
Runnable runnable = new Runnable() {
@Override
public void run() {
publishProgress((int) (written * 100 / size));
}
};
Handler handler = new Handler();
把这条线放在回路里
handler.postDelayed(runnable, x);
在你的活动中
Runnable runnable = new Runnable() {
@Override
public void run() {
publishProgress((int) (written * 100 / size));
}
};
Handler handler = new Handler();
您可以按如下方式使用类:
while ((len = input.read(buf)) > 0)
{
output.write(buf, 0, len);
written += 1024;
// will call publicshProgress after 30 seconds.
new CountDownTimer(30000, 1000)
{
public void onTick(long millisUntilFinished) {}
public void onFinish()
{
//This should be called after x seconds
publishProgress((int) (written * 100 / size));
}
}.start();
}
您可以按如下方式使用类:
while ((len = input.read(buf)) > 0)
{
output.write(buf, 0, len);
written += 1024;
// will call publicshProgress after 30 seconds.
new CountDownTimer(30000, 1000)
{
public void onTick(long millisUntilFinished) {}
public void onFinish()
{
//This should be called after x seconds
publishProgress((int) (written * 100 / size));
}
}.start();
}
此解决方案不会阻止UI线程,因为它使用AsyncTask
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();
Long size=source.length();
InputStream输入=新文件InputStream(源);
OutputStream输出=新文件OutputStream(目标);
//将字节从输入传输到输出
字节[]buf=新字节[1024];
内伦;
长写=0;
而((len=input.read(buf))>0){
输出写入(buf,0,len);
写入+=1024;
Long size=source.length();
InputStream输入=新文件InputStream(源);
OutputStream输出=新文件OutputStream(目标);
//将字节从输入传输到输出
字节[]buf=新字节[1024];
内伦;
长写=0;
而((len=input.read(buf))>0){
输出写入(buf,0,len);
写入+=1024;
(新任务(){
@凌驾
受保护的空位背景(空位…parms){
mEventDataSource.deleteAll();
试一试{
Thread.currentThread().sleep(x*1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//这应该在x秒后调用
出版进度((内部)(书面*100/尺寸);
}
}).execute();
}
input.close();
output.close();
此解决方案不会阻止UI线程,因为它使用AsyncTask
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();
Long size=source.length();
InputStream输入=新文件InputStream(源);
OutputStream输出=新文件OutputStream(目标);
//将字节从输入传输到输出
字节[]buf=新字节[1024];
内伦;
长写=0;
而((len=input.read(buf))>0){
输出写入(buf,0,len);
写入+=1024;
Long size=source.length();
InputStream输入=新文件InputStream(源);
OutputStream输出=新文件OutputStream(目标);
//将字节从输入传输到输出
字节[]buf=新字节[1024];
内伦;
长写=0;
而((len=input.read(buf))>0){
输出写入(buf,0,len);
写入+=1024;
(新任务(){
@凌驾
受保护的空位背景(空位…parms){
mEventDataSource.deleteAll();
试一试{
Thread.currentThread().sleep(x*1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//这应该在x秒后调用
出版进度((内部)(书面*100/尺寸);
}
}).execute();
}
input.close();
output.close();
您可以使用sleep()进行此操作。您可以使用sleep()进行此操作。或者使用ScheduledExecutorService
this,或者使用ScheduledExecutorService
但是Thread.currentThread().sleep(x*1000);
应该是Thread.sleep(x*1000)
我想它们是东西。但是线程.currentThread().sleep(x*1000);
应该是线程.sleep(x*1000);
我想它们是东西。