android phone freeze during the asynctask download

i have done a code that will download the file from internet. but the problem is my phone will freeze(no response) until the download is done. the phone im using is Xperia Arc S and Galaxy S2. anyway to solve this?

    protected void onCreate(Bundle savedInstanceState)

        MyI = new Intent(getApplicationContext(), MaxAppsAct.class);
        MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
        MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(getApplicationContext(), MaxAppsAct.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

        notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices);
        notification.contentIntent = pendingIntent;
        notification.contentView.setTextViewText(, "Downloading...");
        notification.contentView.setProgressBar(, 100, progress, false);
        notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
        notificationManager.notify(42, notification);

        String url = "";
        new DownloadFileAsync().execute(url);

        public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {

        protected void onPreExecute(){

        protected Void doInBackground(String... aurl) {
            int count;
            try {
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();

                int lengthOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile);

                File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
                boolean success = false;
                if (!folder.exists()) {
                    success = folder.mkdirs();
                if (!success) {
                } else {

                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/MaxApps/3d.png");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = != -1) {
                total += count;
                output.write(data, 0, count);

                } catch (Exception e) {}

            return null;

        protected void onProgressUpdate(Integer... progress) {          
            notification.contentView.setProgressBar(, 100, progress[0], false);
            notificationManager.notify(42, notification);

        protected void onPostExecute(String unused) {

            Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
            MyN.tickerText = "Download Complete";
            MyN.number = 1;
            MyN.setLatestEventInfo (getApplicationContext(), "Application Title", "Application Description", MyPI);

            MyNM.notify(1, MyN);


The performance bottleneck seems to be the frequency at which the publishProgress is called. You should devise a way by which you publish the progress less often, without affecting the user experience.


  1. Increase the buffer size to a reasonable value like 16K or 32K
  2. Change the progress publishing mechanism as follows:

    while ((count = != -1) {
        total += count;
        int progressPercent = (int) ((total*100)/lengthOfFile);
        if(progressPercent % 20 == 0){  //publish progress on completion of every 20%
        output.write(data, 0, count);

Need Your Help

Extending .NET MembershipCreateStatus enum

c# enums membership-provider

Basically, what I'm trying to do is add more items to the basic MembershipCreateStatus enum packaged with .NET. However, apparantly you can't do a "partial enum," like so:

MySQL query - limit results of a BLOB to 1 unique element in the blob (row)

php mysql

I've got a query to find abandoned carts that looks like this: