更新部分的代码用红色标出来了。
我把完整代码贴过来了,可以运行的,下载的时候总是出现application not response,去掉更新部分下载是正确的。
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;
}
}
我把完整代码贴过来了,可以运行的,下载的时候总是出现application not response,去掉更新部分下载是正确的。
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;
}
}
myThread和activity里面使用的都是myThread的Looper和MessageQueue,这样为什么不可以呢。
我有3个线程,线程A是UI线程,线程B是HanderThread,线程C是Thread,线程A和线程C使用HandlerThread的Looper和MessageQueue,线程A定义了Handler,线程三发送消息。
我现在是这样实现的,感觉这样应该可以,但是运行没有达到实际效果。
public class A extends Activity
{
private ProgressBar pBar;
private Button bt2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pBar=(ProgressBar)findViewById(R.id.ProgressBar1);
bt2=(Button)findViewById(R.id.Button2);
pBar.setMax(100);
pBar.setProgress(0);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HandlerThread handlerthread=new HandlerThread("test");//B
Looper looper=handlerthread.getLooper();
myHandler myhandler=new myHandler(looper);
C mythread=new C("download",myhandler);
}
}
}
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
Log.v("DownLoad_HTTP", "progress="+msg.arg1);
if(msg.arg1==100)
{
pBar.setProgress(msg.arg1);
looper.quit();
}
else
{
pBar.setProgress(msg.arg1);
}
super.handleMessage(msg);
}
}
}
public class C extends Thread
{
myHandler handler=null;
public C(String name,Handler handler)
{
super(name);
this.handler=handler;
}
int i=0;
while(i++<100)
{
Message message=Message.obtain(handler);
message.agr1=i;
handler.sendMessage(message);
}
}
实际效果progressbar的进度没有更新
你跟我说过了,我用AsyncTask可以的,我想学会用HandlerThread,Hander