Android 如何添加ProgressDialog
我正在从dropbox下载一个文件,这需要几秒钟的时间。我想为下载添加一个Android 如何添加ProgressDialog,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我正在从dropbox下载一个文件,这需要几秒钟的时间。我想为下载添加一个ProgressDialog,但我不知道怎么做 public class DownloadFile extends AsyncTask<Void, Long, Boolean> { DownloadFile(Context context ,DropboxAPI<?> mApi ,String dropboxpath,String sdpath,int pos,int s,ArrayLi
ProgressDialog
,但我不知道怎么做
public class DownloadFile extends AsyncTask<Void, Long, Boolean> {
DownloadFile(Context context ,DropboxAPI<?> mApi ,String dropboxpath,String sdpath,int pos,int s,ArrayList<String> folder) throws DropboxException {
FileOutputStream mFos;
File file=new File(sdpath);
String path = dropboxpath;
try{
mFos = new FileOutputStream(file);
mApi.getFile(path, null, mFos, null);
}catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
公共类下载文件扩展异步任务{
下载文件(上下文上下文、DropboxAPI mApi、字符串dropboxpath、字符串sdpath、int pos、int s、ArrayList文件夹)引发DropboxException{
FileOutputStream mFos;
文件文件=新文件(sdpath);
字符串路径=dropboxpath;
试一试{
mFos=新文件输出流(文件);
获取文件(路径,null,mFos,null);
}捕获(例外e){
//TODO:处理异常
}
}
@凌驾
受保护的布尔doInBackground(Void…params){
//TODO自动生成的方法存根
返回null;
}
}
参见异步任务实际上有4种方法:
onPreExecute()
-您可以在此处执行一些预执行任务doInBackground()
-您可以在这里执行一些背景工作onPostExecute()
-您可以在此处执行执行后任务。例如在ListView中显示数据、更新TextView等onProgressUpdate()
-在后台操作进行时更新UIonPreExecute()
方法中显示进度对话框或进度条,并在onPostExecute()
中关闭(())以同样的方式执行:
public final class DownloadFile extends AsyncTask<Void, Long, Boolean> {
private Context context;
private ProgressDialog progressDialog;
public DownloadFile (Context context) {
this.context = context;
}
/*
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute() {
try {
progressDialog = ProgressDialog.show(context, "", "message", true);
} catch (final Throwable th) {
//TODO
}
}
/*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Boolean doInBackground(Void... arg0) {
//do something
}
@Override
protected void onProgressUpdate(String... progress) {
//do something
super.onProgressUpdate(progress);
}
/*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
} }
public final类下载文件扩展异步任务{
私人语境;
私有进程对话;
公共下载文件(上下文){
this.context=上下文;
}
/*
*@see android.os.AsyncTask#onPreExecute()
*/
@凌驾
受保护的void onPreExecute(){
试一试{
progressDialog=progressDialog.show(上下文“,”消息“,”true);
}捕获(最终可丢弃){
//待办事项
}
}
/*
*@see android.os.AsyncTask#doInBackground(Params[])
*/
@凌驾
受保护的布尔doInBackground(无效…arg0){
//做点什么
}
@凌驾
受保护的void onProgressUpdate(字符串…进度){
//做点什么
super.onProgressUpdate(进度);
}
/*
*@see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@凌驾
受保护的void onPostExecute(布尔结果){
progressDialog.disclose();
} }
使用这个简单的代码@sachin
public class DownloadFile extends AsyncTask<Void, Void, Void> {
Home home;
ProgressDialog dialog = null;
public DownloadFile(Home home) {
// TODO Auto-generated constructor stub
this.home = home;
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
//Call hare method for download
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = ProgressDialog.show(home, "Downloading......", "", true);
}
}
公共类下载文件扩展异步任务{
家;
ProgressDialog=null;
公共下载文件(主页){
//TODO自动生成的构造函数存根
this.home=家;
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
//调用hare方法进行下载
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
dialog.dismise();
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
dialog=ProgressDialog.show(home,“下载…”,“”,true);
}
}
本文对您很有用:
在线程的run()方法中,可以调用如下函数:
public boolean download(String url, String path, String fileName, Handler progressHandler) {
try {
URL sourceUrl = new URL(formatUrl(url));
if (fileName == null || fileName.length() <= 0) {
fileName = sourceUrl.getFile();
}
if (fileName == null || fileName.length() <= 0) {
throw new Exception("EMPTY_FILENAME_NOT_ALLOWED");
}
File targetPath = new File(path);
targetPath.mkdirs();
if (!targetPath.exists()) {
throw new Exception("MISSING_TARGET_PATH");
}
File file = new File(targetPath, fileName);
URLConnection ucon = sourceUrl.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;
int totalSize = ucon.getContentLength();
while ((current = bis.read()) != -1) {
baf.append((byte) current);
// BEGIN - Handler feedback
if (progressHandler != null && (baf.length() % 100) == 0) {
Message msg = progressHandler.obtainMessage();
Bundle b = new Bundle();
if (totalSize > 0) {
b.putInt("total", totalSize);
b.putInt("step", baf.length());
b.putBoolean("working", true);
}
msg.setData(b);
progressHandler.handleMessage(msg);
}
// END
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
// BEGIN - Handler feedback
if (progressHandler != null) {
Message msg = progressHandler.obtainMessage();
Bundle b = new Bundle();
if (totalSize > 0) {
b.putInt("total", 0);
b.putInt("step", 0);
b.putBoolean("working", false);
}
msg.setData(b);
progressHandler.handleMessage(msg);
}
// END
return file.exists();
}
公共布尔下载(字符串url、字符串路径、字符串文件名、处理程序progressHandler){
试一试{
URL sourceUrl=新URL(格式化URL(URL));
if(fileName==null | | fileName.length()0){
b、 putInt(“总计”,0);
b、 putInt(“步骤”,0);
b、 putBoolean(“工作”,假);
}
msg.setData(b);
progressHandler.handleMessage(msg);
}
//结束
返回文件.exists();
}
通过这种方式,您可以更准确地反馈下载的实际进度(每个字节)