标签:
# 主线程阻塞public class SleepActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_test);
}
public void click(View v){
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, "点击了按钮", 1).show();
}
}
public class MessageTestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_test);
}
public void click(View v){
String path = "";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//对连接对象初始化
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if(conn.getResponseCode() == 200){
InputStream in = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(bm);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载图片"
android:onClick="click"/>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
public class MessageTestActivity extends Activity {
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap((Bitmap) msg.obj);//msg.obj是Object类型,所以必须强转
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_test);
}
public void click(View v){
String path = "";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//对连接对象初始化
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if(conn.getResponseCode() == 200){
InputStream in = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
Message msg = new Message();
msg.obj = bm;//Message是可以携带任何Object对象
handler.sendMessage(msg);
} else {
// 这个也属于主线程的操作,所以也不能放到这里
// Toast.makeText(MessageTestActivity.this, "请求失败", 1).show();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class MessageTestActivity extends Activity {
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap((Bitmap) msg.obj);//msg.obj是Object类型,所以必须强转
break;
case 0:
Toast.makeText(MessageTestActivity.this, "请求失败", 1).show();
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_test);
}
public void click(View v){
String path = "http://f.hiphotos.baidu.com/zhidao/pic/item/1e30e924b899a90129bad66e1d950a7b0308f5df.jpg";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//对连接对象初始化
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if(conn.getResponseCode() == 200){
InputStream in = conn.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(in);
Message msg = new Message();
msg.obj = bm;//Message是可以携带任何Object对象
msg.what = 1;
handler.sendMessage(msg);
} else {
// 这个也属于主线程的操作,所以也不能放到这里
// Toast.makeText(MessageTestActivity.this, "请求失败", 1).show();
Message msg = handler.obtainMessage();
msg.what = 0;
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class MessageTestActivity extends Activity { Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: ImageView image = (ImageView) findViewById(R.id.image); image.setImageBitmap((Bitmap) msg.obj);//msg.obj是Object类型,所以必须强转 break; case 0: Toast.makeText(MessageTestActivity.this, "请求失败", 1).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.message_test); } public void click(View v){ Thread t = new Thread(){ public void run() { String path = "http://f.hiphotos.baidu.com/zhidao/pic/item/1e30e924b899a90129bad66e1d950a7b0308f5df.jpg"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //对连接对象初始化 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); if(conn.getResponseCode() == 200){ InputStream in = conn.getInputStream(); Bitmap bm = BitmapFactory.decodeStream(in); Message msg = new Message(); msg.obj = bm;//Message是可以携带任何Object对象 msg.what = 1; handler.sendMessage(msg); } else { Message msg = handler.obtainMessage(); msg.what = 0; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }; t.start(); } }* 这次可以运行了,但是Handler是报警告的,我们应该把handler定义成静态变量,这样,程序任何类都可以调用,以后开发中,会发现相当方便。
public class MessageTestActivity extends Activity { static ImageView image; static MessageTestActivity mta; static Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: image.setImageBitmap((Bitmap) msg.obj);//msg.obj是Object类型,所以必须强转 break; case 0: Toast.makeText(mta, "请求失败", 1).show(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.message_test); image = (ImageView) findViewById(R.id.image); mta = this; } public void click(View v){ Thread t = new Thread(){ public void run() { String path = "http://f.hiphotos.baidu.com/zhidao/pic/item/1e30e924b899a90129bad66e1d950a7b0308f5df.jpg"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //对连接对象初始化 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); if(conn.getResponseCode() == 200){ InputStream in = conn.getInputStream(); Bitmap bm = BitmapFactory.decodeStream(in); Message msg = new Message(); msg.obj = bm;//Message是可以携带任何Object对象 msg.what = 1; handler.sendMessage(msg); } else { Message msg = handler.obtainMessage(); msg.what = 0; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }; t.start(); } }
标签:
原文地址:http://blog.csdn.net/u013475983/article/details/51993553