android--Download

package com.downloader.app;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;


import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;


public class DownloaderActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String TAG = "DownloaderActivity";


private static final String URL_DOWNLOAD = "http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";


private static final int MSG_TYPE = 0x1;


URL url;
private Button btndownload;
private ImageView iv;
private ProgressBar mProgressBar;


private Bitmap mBitmap;
Handler mHandler;
Runnable mRunnable;
private boolean isTrue = true;
MyRunnable mMyRunnable = null;
ProgressDialog mProgressDialog = null;


Timer timer;
TimerTask task;


Handler mHandlerMsg;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


btndownload = (Button) this.findViewById(R.id.btndownload);
iv = (ImageView) this.findViewById(R.id.iv);
mProgressBar = (ProgressBar) this.findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.GONE);


iv.setBackgroundResource(R.drawable.ic_launcher);


btndownload.setOnClickListener(this);
Log.i(TAG, "onCreate name:" + Thread.currentThread().getName());
Log.i(TAG, "onCreate ID:" + Thread.currentThread().getId());


mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case MSG_TYPE:
Log.i(TAG, "handleMessage receive...");
mProgressDialog.dismiss();
iv.setImageBitmap(mBitmap);


Log.i(TAG, "mHandler obj:" + msg.obj);


break;
}
}


};


// Runnable mRunnable = new Runnable() {
// 不是真正开线程,在主线程运行
mRunnable = new Runnable() {// 下载网络资源: 崩溃程序(在主UI开线程访问网络不行)


@Override
public void run() {
// TODO Auto-generated method stub
Log.i(TAG, "thread mRunnable run......");
Log
.i(TAG, "mRunnable name:"
+ Thread.currentThread().getName());
Log.i(TAG, "mRunnable ID:" + Thread.currentThread().getId());
// 下载网络资源: 崩溃程序(在主UI开线程访问网络不行)
/*
* try { url = new URL(URL_DOWNLOAD); //
* 只是创建了一个HttpURLConnection或者URLConnection的实例,并不进行真正的链接操作。
* HttpURLConnection mHttpURLConnection = (HttpURLConnection)
* url .openConnection(); mHttpURLConnection.setDoInput(true);
* mHttpURLConnection.connect(); InputStream mInputStream =
* mHttpURLConnection .getInputStream(); mBitmap =
* BitmapFactory.decodeStream(mInputStream); //
* iv.setImageBitmap(mBitmap); }
*/


}
};


timer = new Timer();
task = new TimerTask() {
int count = 1;


@Override
public void run() {
// TODO Auto-generated method stub


Log.i(TAG, "TimerTask run" + count++);
Log
.i(TAG, "TimerTask name:"
+ Thread.currentThread().getName());
Log.i(TAG, "TimerTask ID:" + Thread.currentThread().getId());
}
};
// start timer
timer.schedule(task, 0, 2000);// delay 0s,2s call,


// HandlerThread(String name)
HandlerThread mHandlerThread = new HandlerThread("handler_thread");
mHandlerThread.start();
// HandlerThread将通过Looper来处理Handler对来中的消息,
// 也就是如果发现Handler中有消息,将在HandlerThread这个线程中进行处理。
// 在子线程中mHandlerMsg发送消息
// Handler mHandlerMsg = new Handler(mHandlerThread.getLooper()) {
mHandlerMsg = new Handler(mHandlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
// switch (msg.what) {


// case MSG_TYPE:
Log.i(TAG, "mHandlerThread mHandlerMsg receive..." + msg.obj);
Log.i(TAG, "mHandlerThread name:"
+ Thread.currentThread().getName());
Log.i(TAG, "mHandlerThread ID:"
+ Thread.currentThread().getId());
// break;
// }
}


};
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {


case R.id.btndownload:
Log.i(TAG, "btndownload onClick...");


mProgressDialog = ProgressDialog.show(this, null,
"downloading...,Please waiting...");
mHandler.post(mRunnable);// start thread://发送消息,启动线程运行
if (mMyRunnable == null) {
Log.i(TAG, "new Thread run...");
// create thread
mMyRunnable = new MyRunnable();
new Thread(mMyRunnable).start();// 启动线程运行,开始下载
}


break;
default:
break;
}


}


// 是真正开线程
public class MyRunnable implements Runnable {
private final static String TAG = "My Runnable ===> ";


@Override
public void run() {
// TODO Auto-generated method stub
// Log.d(TAG, "MyRunnable run...Thread");
// while (isTrue) {
Log.d(TAG, "MyRunnable run...Thread");
Log.i(TAG, "MyRunnable name:" + Thread.currentThread().getName());
Log.i(TAG, "MyRunnable ID:" + Thread.currentThread().getId());
// mProgressDialog=ProgressDialog.show(DownloaderActivity.this,
// " ","下载数据,请稍等 …", isTrue, isTrue, null);
// mProgressBar.setVisibility(0);
// 下载网络资源
try {
url = new URL(URL_DOWNLOAD);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


// 只是创建了一个HttpURLConnection或者URLConnection的实例,并不进行真正的链接操作。
try {
HttpURLConnection mHttpURLConnection = (HttpURLConnection) url
.openConnection();
mHttpURLConnection.setDoInput(true);
mHttpURLConnection.connect();
InputStream mInputStream = mHttpURLConnection.getInputStream();
mBitmap = BitmapFactory.decodeStream(mInputStream);


// 下载完毕, 通知UI


/*
* mProgressBar.setVisibility(View.GONE); 从错误信息不难看出Android禁
* 止其他子线程来更新由UI thread创建的试图。 本例中显示天气信息的title实际是就是一个由UI
* thread所创建的TextView, 所以参试在一个子线程中去更改TextView的时候就出错了。
* 这显示违背了单线程模型的原则:Android UI操作并不是线程安全的并且这些操作必须在UI线 程中执行
*/
mHandler.sendEmptyMessage(MSG_TYPE);
// obtain(Handler h, int what, Object obj)
// Same as obtain(), but sets the values of the target, what,
// and obj members.
mHandler.sendMessage(Message.obtain(mHandler, MSG_TYPE,
"hello,mHandler..."));


Message msg = new Message();
msg.obj = "hello123";
mHandlerMsg.sendMessage(msg);
// msg.obj="hello456";
// mHandlerMsg.sendMessage(msg);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
Log.d(TAG, "MyRunnable stop...");
mMyRunnable = null;
}
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
isTrue = false;
mMyRunnable = null;
Log.i(TAG, "thread onDestroy......");


mHandler.removeCallbacks(mRunnable);// 将线程销毁掉


timer.cancel();// Cancels the Timer and all scheduled tasks.
super.onDestroy();
}
} android--Download_第1张图片

你可能感兴趣的:(android--Download)