UI interface updates must not operate in the UI thread in the Ruanable thread UI can send messages using handler to update the UI
private void Load () {
Loaddate load = new Loaddate ();
Load.execute ("http://h.hiphotos.baidu.com/image/w%3D310/sign=1bc9c0da38292df597c3aa148c305ce2/ C83d70cf3bc79f3d77fbe1c5b8a1cd11728b2928.jpg ");
}
Class Loaddate extends Asynctask<string, Integer, bitmap> {
@Override
protected void OnPreExecute () {
Mydialog.settitle ("Please Wait");
Mydialog.setmessage ("Loading ...");
Mydialog.setcanceledontouchoutside (FALSE);
Mydialog.show ();
}
@Override
Protected Bitmap doinbackground (String ... params) {
URL myfileurl = null;
Bitmap Bitmap = null;
InputStream is = null;
HttpURLConnection conn = null;
try {
Myfileurl = new URL (params[0]);
} catch (Malformedurlexception e) {
E.printstacktrace ();
}
try {
conn = (httpurlconnection) myfileurl
. OpenConnection ();
Conn.setdoinput (TRUE);
Conn.connect ();
Conn.setconnecttimeout (3000);
Conn.setreadtimeout (3000);
is =conn.getinputstream ();
Bitmap =bitmapfactory.decodestream (IS);
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}finally{
try {
if (is = null) {
Is.close ();
}
IF (conn! = null) {
Conn.disconnect ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
StartTime (bitmap);
return bitmap;
}
Handler myhandler=new Handler () {
public void Handlemessage (Message msg) {
if (msg.what==1) {
Toast.maketext (mainactivity.this, "Connection server timeout" +msg.obj, 1). Show ();
}
};
};
private void StartTime (final Bitmap BM) {
Runnable run=new Runnable () {
Boolean isrun=true;
Long Enabletime;
Long Starttime=system.currenttimemillis ();
@Override
public void Run () {
System.out.println (bm+ "---BM----");
while (Isrun) {
Enabletime=system.currenttimemillis ()-starttime;
if (enabletime>=3000 &&bm==null) {
Message message = new Message ();
Message.what = 1;
Message.obj= "Demo";
Myhandler.sendmessage (message);
Toast.maketext (mainactivity.this, "Connection Server timeout", 1). Show ();
Isrun=false;
}
try{
Thread.Sleep (50);
}catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Timer thread sleep Ex:" +e.tostring ());
}
}
SYSTEM.OUT.PRINTLN ("Timer thread run: End time: "+enabletime);
}
};
Looper.prepare ();
New Thread (Run). Start ();
}
@Override
protected void OnPostExecute (Bitmap result) {
Image.setimagebitmap (result);
Mydialog.cancel ();
System.out.println (result+ "---");
}
}
}