Android 如何在I';我不在主线程中
我正在尝试从URL下载文件。如果下载失败(无论原因如何),我希望应用程序在一小时后重试 由于下载是在它自己的线程中(不是主线程),我似乎无法启动新的倒计时 我不想阻止下载线程,所以我尝试使用倒计时 还有别的办法吗 提前谢谢Android 如何在I';我不在主线程中,android,multithreading,countdowntimer,Android,Multithreading,Countdowntimer,我正在尝试从URL下载文件。如果下载失败(无论原因如何),我希望应用程序在一小时后重试 由于下载是在它自己的线程中(不是主线程),我似乎无法启动新的倒计时 我不想阻止下载线程,所以我尝试使用倒计时 还有别的办法吗 提前谢谢 private class Downloader extends AsyncTask<Object, Void, File> { @Override protected File doInBackground(Object... params)
private class Downloader extends AsyncTask<Object, Void, File>
{
@Override
protected File doInBackground(Object... params)
{
Context context = (Context) params[0];
String strUrl = (String) params[1];
String strFileName = (String) params[2];
return download(context, strUrl, strFileName);
}
/**
* Downloads files in a separate thread. Adds notification of download to status bar.
*/
public File download(final Context context, final String url, final String fileName)
{
boolean isVideoLoaded=false;
try
{
URL urlObj = new URL(url);
URLConnection con = urlObj.openConnection();
BufferedInputStream bis = new BufferedInputStream(con.getInputStream(), BUFFER_SIZE);
FileOutputStream fos = new FileOutputStream(retFile);
byte[] bArray = new byte[BUFFER_SIZE];
int current = 0;
int read = 0;
while(current != -1)
{
fos.write(bArray,0,current);
current = bis.read(bArray, 0, BUFFER_SIZE);
read = read + current;
}
fos.close();
bis.close();
isVideoLoaded = true;
strFileName = retFile.getAbsolutePath();
}
catch(Exception ioe)
{
Log.d("Downloader.download", "Error: " + ioe.toString(), ioe);
}
}
if (!isVideoLoaded) // if video is still not loaded
{
// sleep then try again
new CountDownTimer(15000, 2000)
{
public void onFinish()
{
Downloader dh = new Downloader();
Object params[] = new Object[3];
params[0] = context;
params[1] = url;
params[2] = fileName;*/
dh.execute(params);
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}.start();
}
return retFile;
}
protected void onPostExecute(File file) {
// display downloaded file
}
私有类下载程序扩展异步任务
{
@凌驾
受保护的文件doInBackground(对象…参数)
{
上下文=(上下文)参数[0];
字符串strUrl=(字符串)参数[1];
字符串strFileName=(字符串)参数[2];
返回下载(上下文、strUrl、strFileName);
}
/**
*在单独的线程中下载文件。将下载通知添加到状态栏。
*/
公共文件下载(最终上下文、最终字符串url、最终字符串文件名)
{
布尔值isVideoLoaded=false;
尝试
{
URL urlObj=新URL(URL);
URLConnection con=urlObj.openConnection();
BufferedInputStream bis=新的BufferedInputStream(con.getInputStream(),BUFFER_SIZE);
FileOutputStream fos=新的FileOutputStream(retFile);
byte[]baray=新字节[缓冲区大小];
int电流=0;
int read=0;
while(当前!=-1)
{
fos.写入(bArray,0,当前);
当前=双读取(bArray,0,缓冲区大小);
读取=读取+当前;
}
fos.close();
二、关闭();
isVideoLoaded=true;
strFileName=retFile.getAbsolutePath();
}
捕获(异常ioe)
{
Log.d(“Downloader.download”,“错误:+ioe.toString(),ioe”);
}
}
if(!isVideoLoaded)//如果仍然没有加载视频
{
//睡觉,然后再试一次
新倒计时器(15000,2000)
{
公共无效onFinish()
{
Downloader dh=新的Downloader();
对象参数[]=新对象[3];
参数[0]=上下文;
参数[1]=url;
params[2]=文件名*/
dh.execute(params);
}
@凌驾
公共void onTick(长毫秒未完成){
//TODO自动生成的方法存根
}
}.start();
}
返回文件;
}
受保护的void onPostExecute(文件){
//显示下载的文件
}
更改您的onPostExecute
,以便它调用主活动以表示下载失败(应该能够通过MyMainActivity.this.someMethod()
)然后主活动可以创建一个新的异步任务(您不能重用旧任务)并表示它应该延迟启动
执行此操作的一个选项是向AsyncTask添加一个构造函数,以便向其传递延迟参数,然后AsyncTask可以将延迟参数用作doInBackground
方法的一部分
在
doInBackground
内部,只需使用线程等待。sleep()
您可以创建一个计时器,在UI线程上开始下载。
在UI类中,使用此函数
private StartOneHourTimer()
{
new CountDownTimer(10000 * 3600, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
m_downloader = new Downloader(..);
m_downloader.execute(..);
}
}.start();
}
在下载程序onPostExecute()中,
启动一个新的计时器,该计时器将在1小时后过期
protected void onPostExecute(File file)
{
if (!isVideoLoaded)
startOneHourTimer();
else
//Call your normal UI code here
}