1313
1414import android .app .Activity ;
1515import android .app .ProgressDialog ;
16+ import android .content .Context ;
1617import android .graphics .Bitmap ;
1718import android .graphics .BitmapFactory ;
1819import android .os .Bundle ;
1920import android .os .Handler ;
21+ import android .os .Message ;
2022import android .view .View ;
2123import android .widget .ImageView ;
2224
2325public class ThreadsLifecycleActivity extends Activity {
2426 // Static so that the thread access the latest attribute
2527 private static ProgressDialog dialog ;
26- private static ImageView imageView ;
2728 private static Bitmap downloadBitmap ;
2829 private static Handler handler ;
30+ private ImageView imageView ;
2931 private Thread downloadThread ;
3032
3133 /** Called when the activity is first created. */
@@ -35,9 +37,18 @@ public void onCreate(Bundle savedInstanceState) {
3537 super .onCreate (savedInstanceState );
3638 setContentView (R .layout .main );
3739 // Create a handler to update the UI
38- handler = new Handler ();
40+ handler = new Handler () {
41+ @ Override
42+ public void handleMessage (Message msg ) {
43+ imageView .setImageBitmap (downloadBitmap );
44+ dialog .dismiss ();
45+ }
46+
47+ };
3948 // get the latest imageView after restart of the application
4049 imageView = (ImageView ) findViewById (R .id .imageView1 );
50+ Context context = imageView .getContext ();
51+ System .out .println (context );
4152 // Did we already download the image?
4253 if (downloadBitmap != null ) {
4354 imageView .setImageBitmap (downloadBitmap );
@@ -110,7 +121,8 @@ public void run() {
110121 e .printStackTrace ();
111122 }
112123 downloadBitmap = downloadBitmap ("http://www.devoxx.com/download/attachments/4751369/DV11" );
113- handler .post (new MyRunnable ());
124+ // Updates the user interface
125+ handler .sendEmptyMessage (0 );
114126 } catch (IOException e ) {
115127 e .printStackTrace ();
116128 } finally {
@@ -119,11 +131,4 @@ public void run() {
119131 }
120132 }
121133
122- static public class MyRunnable implements Runnable {
123- public void run () {
124- imageView .setImageBitmap (downloadBitmap );
125- dialog .dismiss ();
126- }
127- }
128-
129134}
0 commit comments