Android Open Source - android_tutorial_projects Threads Lifecycle Activity






From Project

Back to project page android_tutorial_projects.

License

The source code is released under:

Copyright (c) 2013, Uthcode All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...

If you think the Android project android_tutorial_projects listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.uthcode.threadslifecycle;
/*from w  ww  . j ava 2 s.  co  m*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class ThreadsLifecycleActivity extends Activity {

    public static ProgressDialog dialog;
    private static Bitmap downloadBitmap;
    private static Handler handler;
    private ImageView imageView;
    private Thread downloadThread;


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

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                imageView.setImageBitmap(downloadBitmap);
                dialog.dismiss();
            }
        };


        imageView = (ImageView) findViewById(R.id.imageView1);
        Context context = imageView.getContext();
        System.out.println(context);

        if (downloadBitmap != null) {
            imageView.setImageBitmap(downloadBitmap);
        }


        downloadThread = (Thread)  getLastNonConfigurationInstance();
        if (downloadThread != null && downloadThread.isAlive()) {
            dialog = ProgressDialog.show(this, "Download", "Downloading");
        }
    }

    public void resetPicture(View view) {
       if (downloadBitmap != null) {
          downloadBitmap = null;
       }
       imageView.setImageResource(R.drawable.ic_launcher);
    }

    public void downloadPicture(View view) {
       dialog = ProgressDialog.show(this, "Download", "Downloading");
       downloadThread = new MyThread();
       downloadThread.start();
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return downloadThread;
    }

    @Override
    protected void onDestroy() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
        super.onDestroy();
    }

    static private Bitmap downloadBitmap(String url) throws IOException {
        HttpUriRequest request = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();

        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            byte[] bytes = EntityUtils.toByteArray(entity);
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            return bitmap;
        } else
            throw new IOException("Download failed with HTTP response code" + statusCode + "-" + statusLine.getReasonPhrase());
    }

    static public class MyThread extends Thread {
        @Override
        public void run() {
            try {
                try {
                    new Thread().sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                downloadBitmap = downloadBitmap("http://www.devoxx.com/download/attachments/4751369/DV11");
                handler.sendEmptyMessage(0);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

}




Java Source Code List

com.uthcode.Main.java
com.uthcode.alertexampleactivity.MainActivity.java
com.uthcode.asynctask.ReadWebpageAsyncTask.java
com.uthcode.exampledatepicker.MainActivity.java
com.uthcode.expandablelistview.Group.java
com.uthcode.expandablelistview.MainActivity.java
com.uthcode.expandablelistview.MyExpandableListAdapter.java
com.uthcode.imagepicker.ImagePickActivity.java
com.uthcode.implicitintents.CallIntentsActivity.java
com.uthcode.intents.MainActivity.java
com.uthcode.intents.ResultActivity.java
com.uthcode.lifecycleactivity.MainActivity.java
com.uthcode.lifecycleactivity.SecondActivity.java
com.uthcode.lifecycleactivity.TracerActivity.java
com.uthcode.mylistactivity.MyListActivity.java
com.uthcode.mylistactivity.MyPerformanceArrayAdapter.java
com.uthcode.mylistusingmodels.InteractiveArrayAdapter.java
com.uthcode.mylistusingmodels.Model.java
com.uthcode.mylistusingmodels.MyListActivity.java
com.uthcode.overview.OverviewActivity.java
com.uthcode.parsejson.MainActivity.java
com.uthcode.progresstest.MainActivity.java
com.uthcode.registeredintent.BrowserActivity.java
com.uthcode.rssreader.DetailFragment.java
com.uthcode.rssreader.MyListFragment.java
com.uthcode.rssreader.RssfeedActivity.java
com.uthcode.simplecursoradapter.MainActivity.java
com.uthcode.tempconverter.ConverterUtil.java
com.uthcode.tempconverter.MainActivity.java
com.uthcode.testdatabaseactivity.Comment.java
com.uthcode.testdatabaseactivity.CommentsDataSource.java
com.uthcode.testdatabaseactivity.MainActivity.java
com.uthcode.testdatabaseactivity.MySQLiteHelper.java
com.uthcode.threadslifecycle.ThreadsLifecycleActivity.java
com.uthcode.todolist.ToDoContentProvider.java
com.uthcode.todolist.ToDoItemAdapter.java
com.uthcode.todolist.ToDoItem.java
com.uthcode.todolist.ToDoListActivity.java
com.uthcode.todolist.ToDoListItemView.java
com.uthcode.todos.TodoDetailActivity.java
com.uthcode.todos.TodosOverviewActivity.java
com.uthcode.todos.contentprovider.MyTodoContentProvider.java
com.uthcode.todos.database.TodoDatabaseHelper.java
com.uthcode.todos.database.TodoTable.java
com.uthcode.twolistitemsactivity.MyTwoListItemsActivity.java