你好我正在写一个下载项目,我就在这里。
@Override
protected void onProgressUpdate(Integer... progress) {
}
//I'm using in my mainactivity like that
Download download = new Download();
download.execute("http://");
我希望在更新时取得进展,是的,我可以使用onProgressUpdate做到这一点,但我想知道我可以从我的主要活动得到它我的意思是我称之为课程。我喜欢那种有活力的课程,因为我可以轻松地将它们用于我的每个项目。谢谢你,原谅我的语法。
答案 0 :(得分:0)
我可以在这里看到两个选项:
您可以使用构造函数将要显示进度的对象传递给AsyncTask,然后存储它:
private ProgressObject mObject;
public void MyAsyncTask(ProgressObject object) {
mObject = object
}
onProgress()方法的更新:
object.setProgress(); //assuming the ProgressObject has a method setProgress() obviously
或者你可以设置某种听众:
public interface ProgressListener() {
public void onProgress(int progress);
}
private mProgressListener;
public void MyAsyncTask(ProgressListener listener) {
mProgressListener = listener;
}
然后在onProgress()方法中使用它:
mProgressListener.onProgress(80);
只是一些例子,并不多,但我希望有所帮助。
答案 1 :(得分:0)
使用此
class DownloadFileAsync extends AsyncTask<String, String, String> {
@SuppressWarnings("deprecation")
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
File root = android.os.Environment.getExternalStorageDirectory();
//
File dir = new File (root.getAbsolutePath()+"/downoad"); //make ur folder to put download
if(dir.exists()==false) {
dir.mkdirs();
}
File file = new File(dir, "enter file name");
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1)
{
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
Toast.makeText(DisplayActivity.this,"Successfully downloaded in phone memory.", Toast.LENGTH_SHORT).show();
}
}
如何打电话?
new DownloadFileAsync().execute("your URL");
答案 2 :(得分:0)
点击此链接下载并取得进展