我不能建立我的项目,因为日食给错误
下面是按钮单击事件谁开始下载
button.setOnClickListener(新View.OnClickListener(){ @覆盖 公共无效的onClick(视图v){ ProgressDialog mProgressDialog; mProgressDialog =新ProgressDialog(Test.this); mProgressDialog.setMessage(Pobieranie); mProgressDialog.setIndeterminate(假); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); DownloadFile downloadFile =新DownloadFile(); downloadFile.execute(http://google.com); }});
和AsyncTask的功能
私有类DownloadFile扩展的AsyncTask<字符串,整数,字符串> { [...] @覆盖 在preExecute保护无效(){ super.on preExecute(); mProgressDialog.show(); //这里日食告诉mProgressDialog解决不了 } @覆盖 保护无效onProgressUpdate(整数...进度){ super.onProgressUpdate(进度); mProgressDialog.setProgress(进展[0]); //这里日食告诉mProgressDialog解决不了 }}
那么,我应该插入code谁创建我的进度对话框?
解决方案 嘿,你需要提供数在此输入code 私有类DownloadFile扩展的AsyncTask<字符串,整数,字符串> { ProgressDialog mProgressDialog; 字符串文件名= NULL; @覆盖 在preExecute保护无效(){ super.on preExecute(); //创建进度 mProgressDialog =新ProgressDialog(Task.this); //设置你的进度标题 mProgressDialog.setTitle(下载); mProgressDialog.setIcon(R.drawable.dwnload); //设置您的进度信息 mProgressDialog.setMessage(更新应用程序版本,请等一下!); mProgressDialog.setIndeterminate(假); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //显示进度 mProgressDialog.setCancelable(假); // mProgressDialog.setCanceledOnTouchOutside(假); mProgressDialog.show(); } @覆盖 保护字符串doInBackground(字符串... SURL){ 尝试{ 字符串apkurl =http://google.com/\"+fileName; 网址URL =新的URL(apkurl); HttpURLConnection的C =(HttpURLConnection类)网址 .openConnection(); c.setRequestMethod(GET); c.setDoOutput(真); c.connect(); INT文件长度= c.getContentLength(); //文件文件=新的文件(路径); 如果(file.exists()) { file.delete(); } file.mkdirs(); 文件OUTPUTFILE =新的文件(文件,文件名); 最终的FileOutputStream FOS =新的FileOutputStream(OUTPUTFILE); 最后InputStream为= c.getInputStream(); 字节的数据[] =新的字节[1024]; 总长= 0; 诠释计数; 而((计数= is.read(数据))!= - 1){ 总+ =计数; //发布进度 publishProgress((int)的(总* 100 /文件长度)); fos.write(数据0,计); } //关闭连接 fos.flush(); fos.close(); is.close(); } } 赶上(例外五){ //错误日志 Log.e(错误,e.getMessage()); e.printStackTrace(); } 返回null; } @覆盖 保护无效onPostExecute(字符串结果){ // TODO自动生成方法存根 mProgressDialog.dismiss(); } @覆盖 保护无效onProgressUpdate(整数...进度){ super.onProgressUpdate(进度); //更新进度 mProgressDialog.setProgress(进展[0]); }}
I can't build my project because eclipse giving error
Here is button click event who starting download
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog mProgressDialog;
mProgressDialog = new ProgressDialog(Test.this);
mProgressDialog.setMessage("Pobieranie");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://google.com");
}
});
and AsyncTask function
private class DownloadFile extends AsyncTask<String, Integer, String> {
[...]
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show(); // Here eclipse tell that "mProgressDialog cannot be resolved"
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]); // Here eclipse tell that "mProgressDialog cannot be resolved"
}
}
So where I should insert code who create my progress dialog?
解决方案Hey you need to provide count
enter code here
private class DownloadFile extends AsyncTask<String, Integer, String> {
ProgressDialog mProgressDialog;
String fileName=null;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create ProgressBar
mProgressDialog = new ProgressDialog(Task.this);
// Set your ProgressBar Title
mProgressDialog.setTitle("Downloads");
mProgressDialog.setIcon(R.drawable.dwnload);
// Set your ProgressBar Message
mProgressDialog.setMessage("Updating App Version, Please Wait!");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// Show ProgressBar
mProgressDialog.setCancelable(false);
// mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... sUrl) {
try {
String apkurl = "http://google.com/"+fileName;
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url
.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int fileLength = c.getContentLength();
//File file = new File(PATH);
if(file.exists())
{
file.delete();
}
file.mkdirs();
File outputFile = new File(file, fileName);
final FileOutputStream fos = new FileOutputStream(outputFile);
final InputStream is = c.getInputStream();
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = is.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int) (total * 100 / fileLength));
fos.write(data, 0, count);
}
// Close connection
fos.flush();
fos.close();
is.close();
}
}
catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
mProgressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
// Update the ProgressBar
mProgressDialog.setProgress(progress[0]);
}
}