我现在有一个acitivity,里面有ProgressBar和Button,点击button就开始下载,想在下载的同时更新ProgressBar的进度,不知道怎么去更新进度?
activity代码
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HandlerThread myThread=new myThread("downloadthread",url.getText().toString(),"DownLoad"+File.separator,"1.mp3");
myThread.start();
//myThread.
}
});
}
class myHandler extends Handler
{
public myHandler()
{
}
public myHandler(Looper looper)
{
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
pBar.setProgress(msg.arg1);
super.handleMessage(msg);
}
}
线程代码
package zxy.study.DownLoad;import zxy.study.DownLoad.DownLoad_HTTPActivity.myHandler;
import android.os.HandlerThread;
import android.os.Message;public class myThread extends HandlerThread {
String urlString=null;
String pathString=null;
String filenameString=null;
int progress=0;
public myThread(String name)
{
super(name);
}
public myThread(String ThreadName,String urlString,String path,String filename)
{
super(ThreadName);
this.urlString=urlString;
this.pathString=path;
this.filenameString=filename;
}
@Override
public void run() {
// TODO Auto-generated method stub
HTTPDownLoader httpDownLoader=new HTTPDownLoader();
httpDownLoader.DownLoad(urlString, pathString, filenameString);
while(progress!=100)
{
Message message=new Message();
message.arg1=progress+1;
sendMessage(message);//不知道怎么把进度更新到ProgressBar了
}
//super.run();
}
}HTTPDownLoader代码
public class HTTPDownLoader {
public int DownLoad(String urlstr,String path,String fileName)
{
InputStream iStream=null;
try {
FileUtility fileUtility=new FileUtility();
URL url = new URL(urlstr);
HttpURLConnection urlCon=(HttpURLConnection)url.openConnection();
iStream=urlCon.getInputStream();
urlCon.getContentLength();//获得下载文件的大小
if(fileUtility.isfileExist(path+fileName))
return 1;
File file=fileUtility.write2SDcardFromInputStream(path, fileName, iStream);
if(file==null)
return -1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
finally{
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
}
fileutility代码
public class FileUtility {
private String SDPATH;
public String getSDPath()
{
return SDPATH;
}
public FileUtility()
{
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
}
public File createSDFile(String fileName)
{
File file=new File(SDPATH+fileName);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
public File createDir(String dirName)
{
File dir=new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
public boolean isfileExist(String fileName)
{
File file=new File(SDPATH+fileName);
return file.exists();
}
public File write2SDcardFromInputStream(String path,String name,InputStream inputstream)
{
File file=null;
OutputStream os=null;
try {
createDir(path);
file = createSDFile(path+name);
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
while(inputstream.read(buffer)!=-1)
{
os.write(buffer);
}
os.flush();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(os!=null)
os.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}
activity代码
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
HandlerThread myThread=new myThread("downloadthread",url.getText().toString(),"DownLoad"+File.separator,"1.mp3");
myThread.start();
//myThread.
}
});
}
class myHandler extends Handler
{
public myHandler()
{
}
public myHandler(Looper looper)
{
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
pBar.setProgress(msg.arg1);
super.handleMessage(msg);
}
}
线程代码
package zxy.study.DownLoad;import zxy.study.DownLoad.DownLoad_HTTPActivity.myHandler;
import android.os.HandlerThread;
import android.os.Message;public class myThread extends HandlerThread {
String urlString=null;
String pathString=null;
String filenameString=null;
int progress=0;
public myThread(String name)
{
super(name);
}
public myThread(String ThreadName,String urlString,String path,String filename)
{
super(ThreadName);
this.urlString=urlString;
this.pathString=path;
this.filenameString=filename;
}
@Override
public void run() {
// TODO Auto-generated method stub
HTTPDownLoader httpDownLoader=new HTTPDownLoader();
httpDownLoader.DownLoad(urlString, pathString, filenameString);
while(progress!=100)
{
Message message=new Message();
message.arg1=progress+1;
sendMessage(message);//不知道怎么把进度更新到ProgressBar了
}
//super.run();
}
}HTTPDownLoader代码
public class HTTPDownLoader {
public int DownLoad(String urlstr,String path,String fileName)
{
InputStream iStream=null;
try {
FileUtility fileUtility=new FileUtility();
URL url = new URL(urlstr);
HttpURLConnection urlCon=(HttpURLConnection)url.openConnection();
iStream=urlCon.getInputStream();
urlCon.getContentLength();//获得下载文件的大小
if(fileUtility.isfileExist(path+fileName))
return 1;
File file=fileUtility.write2SDcardFromInputStream(path, fileName, iStream);
if(file==null)
return -1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
finally{
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
}
fileutility代码
public class FileUtility {
private String SDPATH;
public String getSDPath()
{
return SDPATH;
}
public FileUtility()
{
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
}
public File createSDFile(String fileName)
{
File file=new File(SDPATH+fileName);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
public File createDir(String dirName)
{
File dir=new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
public boolean isfileExist(String fileName)
{
File file=new File(SDPATH+fileName);
return file.exists();
}
public File write2SDcardFromInputStream(String path,String name,InputStream inputstream)
{
File file=null;
OutputStream os=null;
try {
createDir(path);
file = createSDFile(path+name);
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
while(inputstream.read(buffer)!=-1)
{
os.write(buffer);
}
os.flush();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(os!=null)
os.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}
不知道 是不是要 这种效果
seekBar.setSecondaryProgress((int) (process*100));
package com.yarin.android.Examples_04_21;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;public class Activity01 extends Activity
{
//声明ProgressBar对象
private ProgressBar m_ProgressBar;
private ProgressBar m_ProgressBar2;
private Button mButton01;
protected static final int GUI_STOP_NOTIFIER = 0x108;
protected static final int GUI_THREADING_NOTIFIER = 0x109;
public int intCounter=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//设置窗口模式,,因为需要显示进度条在标题栏
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.main);
//取得ProgressBar
m_ProgressBar = (ProgressBar) findViewById(R.id.ProgressBar01);
m_ProgressBar2= (ProgressBar) findViewById(R.id.ProgressBar02);
mButton01 = (Button)findViewById(R.id.Button01);
m_ProgressBar.setIndeterminate(false);
m_ProgressBar2.setIndeterminate(false);
//当按钮按下时开始执行,
mButton01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//设置ProgressBar为可见状态
m_ProgressBar.setVisibility(View.VISIBLE);
m_ProgressBar2.setVisibility(View.VISIBLE);
//设置ProgressBar的最大值
m_ProgressBar.setMax(100);
m_ProgressBar2.setMax(100);
//设置ProgressBar当前值
m_ProgressBar.setProgress(0);
m_ProgressBar2.setProgress(0); //通过线程来改变ProgressBar的值
new Thread(new Runnable() {
public void run()
{
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(1000);
intCounter = (i + 1) * 20;
if (i == 5)
{
Message m = new Message(); m.what = Activity01.GUI_STOP_NOTIFIER;
Activity01.this.myMessageHandler.sendMessage(m);
break;
}
else
{
Message m = new Message();
m.what = Activity01.GUI_THREADING_NOTIFIER;
Activity01.this.myMessageHandler.sendMessage(m);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}).start();
}
});
} Handler myMessageHandler = new Handler()
{
// @Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
//ProgressBar已经是对大值
case Activity01.GUI_STOP_NOTIFIER:
m_ProgressBar.setVisibility(View.GONE);
m_ProgressBar2.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case Activity01.GUI_THREADING_NOTIFIER:
if (!Thread.currentThread().isInterrupted())
{
// 改变ProgressBar的当前值
m_ProgressBar.setProgress(intCounter);
m_ProgressBar2.setProgress(intCounter);
// 设置标题栏中前景的一个进度条进度值
setProgress(intCounter*100);
// 设置标题栏中后面的一个进度条进度值
setSecondaryProgress(intCounter*100);//
}
break;
}
super.handleMessage(msg);
}
};
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/ProgressBar02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:visibility="gone"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>
package zxy.study.DownLoad;import java.io.File;
import java.io.IOException;import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
activity部分:
public class DownLoad_HTTPActivity extends Activity {
private EditText url;
private Button bt1;
private Button bt2;
private ProgressBar pBar;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
url=(EditText)findViewById(R.id.editText1);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
pBar=(ProgressBar)findViewById(R.id.progressBar1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pBar.setMax(100);
pBar.setProgress(0);
Thread myThread=new myThread("downloadthread",url.getText().toString(),"DownLoad"+File.separator,"1.mp3");
myThread.start();
}
});
}
class myHandler extends Handler
{
public myHandler()
{
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
pBar.setProgress(msg.arg1);
super.handleMessage(msg);
}
}
}
Thread部分:
public class myThread extends Thread {
String urlString=null;
String pathString=null;
String filenameString=null;
static int progress=0;
String SDPATH;
HttpURLConnection urlCon=null;
public myThread(String name)
{
super(name);
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
}
public myThread(String ThreadName,String urlString,String path,String filename)
{
super(ThreadName);
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
this.urlString=urlString;
this.pathString=path;
this.filenameString=filename;
}
@Override
public void run() {
// TODO Auto-generated method stub
DownLoad(urlString, pathString, filenameString);
}
public int DownLoad(String urlstr,String path,String fileName)
{
InputStream iStream=null;
try {
FileUtility fileUtility=new FileUtility();
URL url = new URL(urlstr);
urlCon=(HttpURLConnection)url.openConnection();
iStream=urlCon.getInputStream();
urlCon.getContentLength();//获得下载文件的大小
if(fileUtility.isfileExist(path+fileName))
return 1;
File file=fileUtility.write2SDcardFromInputStream(path, fileName, iStream);
if(file==null)
return -1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
finally{
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
public File createSDFile(String fileName)
{
File file=new File(SDPATH+fileName);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
public File createDir(String dirName)
{
File dir=new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
public boolean isfileExist(String fileName)
{
File file=new File(SDPATH+fileName);
return file.exists();
}
public File write2SDcardFromInputStream(String path,String name,InputStream inputstream)
{
File file=null;
OutputStream os=null;
int len;
try {
createDir(path);
file = createSDFile(path+name);
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
while((len=inputstream.read(buffer))!=-1)
{
os.write(buffer);
Message message=new Message();
progress=progress+len;
message.arg1=progress/urlCon.getContentLength()*100;
sendMessage(arg1);//?????
}
}
os.flush();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(os!=null)
os.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}
activity部分
public class DownLoad_HTTPActivity extends Activity {
private EditText url;
private Button bt1;
private Button bt2;
private ProgressBar pBar;
public Handler hlerHandler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
url=(EditText)findViewById(R.id.editText1);
bt1=(Button)findViewById(R.id.button1);
bt2=(Button)findViewById(R.id.button2);
pBar=(ProgressBar)findViewById(R.id.progressBar1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pBar.setMax(100);
pBar.setProgress(0);
myThread mythread=new myThread("downloadthread",url.getText().toString(),"DownLoad"+File.separator,"1.mp3");
mythread.start();
Looper looper=mythread.getLooper();
hlerHandler=new myHandler(looper);
}
});
}
class myHandler extends Handler
{
Looper looper;
public myHandler(Looper looper)
{
super(looper);
this.looper=looper;
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.arg1==100)
looper.quit();
else
pBar.setProgress(msg.arg1);
super.handleMessage(msg);
}
}
}
thread部分:
public class myThread extends HandlerThread {
String urlString=null;
String pathString=null;
String filenameString=null;
static int progress=0;
String SDPATH;
HttpURLConnection urlCon=null;
public myThread(String name)
{
super(name);
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
}
public myThread(String ThreadName,String urlString,String path,String filename)
{
super(ThreadName);
SDPATH=Environment.getExternalStorageDirectory()+File.separator;
this.urlString=urlString;
this.pathString=path;
this.filenameString=filename;
}
@Override
public void run() {
// TODO Auto-generated method stub
DownLoad(urlString, pathString, filenameString);
}
public int DownLoad(String urlstr,String path,String fileName)
{
InputStream iStream=null;
try {
FileUtility fileUtility=new FileUtility();
URL url = new URL(urlstr);
urlCon=(HttpURLConnection)url.openConnection();
iStream=urlCon.getInputStream();
urlCon.getContentLength();//获得下载文件的大小
if(fileUtility.isfileExist(path+fileName))
return 1;
File file=fileUtility.write2SDcardFromInputStream(path, fileName, iStream);
if(file==null)
return -1;
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
finally{
try {
iStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return 0;
}
public File createSDFile(String fileName)
{
File file=new File(SDPATH+fileName);
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return file;
}
public File createDir(String dirName)
{
File dir=new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
public boolean isfileExist(String fileName)
{
File file=new File(SDPATH+fileName);
return file.exists();
}
public File write2SDcardFromInputStream(String path,String name,InputStream inputstream)
{
File file=null;
OutputStream os=null;
int len;
try {
createDir(path);
file = createSDFile(path+name);
os=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];
while((len=inputstream.read(buffer))!=-1)
{
os.write(buffer);
Message message=new Message();
progress=progress+len;
message.arg1=progress/urlCon.getContentLength()*100;
Looper llLooper = this.getLooper();
Handler hler = new Handler( llLooper );
hler.sendMessage( message );
}
os.flush();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(os!=null)
os.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
}