Example usage for android.os AsyncTask AsyncTask

List of usage examples for android.os AsyncTask AsyncTask

Introduction

In this page you can find the example usage for android.os AsyncTask AsyncTask.

Prototype

public AsyncTask() 

Source Link

Document

Creates a new asynchronous task.

Usage

From source file:Main.java

public static void setFBImage(final String fbid, final Context context, final ImageView v) {
    new AsyncTask<String, Void, Bitmap>() {

        @Override/*from   w  w  w  . j  a v  a  2s  .  c  om*/
        protected Bitmap doInBackground(String... strings) {
            File img = new File(context.getFilesDir() + "/profile.jpg");
            Bitmap bmp = null;
            if (img.exists()) {
                try {
                    bmp = BitmapFactory.decodeFile(img.getAbsolutePath());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            } else {
                try {
                    URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid)
                            + "/picture?type=large&redirect=true&width=400&height=400");
                    bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
                    FileOutputStream fOut = new FileOutputStream(img);
                    bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
                    fOut.flush();
                    fOut.close();
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
            return bmp;
        }

        @Override
        protected void onPostExecute(Bitmap img) {
            if (img != null) {
                v.setImageBitmap(img);
            }
        }
    }.execute();
}

From source file:Main.java

public static void smoothToTransparentFromColor(final View view, final int color) {
    Date firstDate = new Date();
    final long firstTime = firstDate.getTime();
    executeAsyncTask(new AsyncTask<Void, Integer, Void>() {
        int n = 1, t = 4000;
        boolean increaseN;

        @Override/*from w w w.  j a  va 2 s  .c om*/
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                Date currentDate = new Date();
                long diffTime = currentDate.getTime() - firstTime;

                double y = getCosY(diffTime);
                int alpha = (int) (y * Color.alpha(color));
                int resultColor = setAlphaComponent(color, alpha);
                if (alpha < 0.038 * 255) {
                    publishProgress(0);
                    this.cancel(true);
                    return null;
                }
                publishProgress(resultColor, alpha);
                try {
                    Thread.sleep(38);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            view.setBackgroundColor(values[0]);
        }
    });
}

From source file:Main.java

/**
 * Run the Runnable "delayed" by using an AsyncTask to first require a new
 * thread and only then, in onPostExecute, run the Runnable on the UI thread.
 * @param runnable Runnable to run on UI thread.
 *///from  w  ww.  j a  v  a 2  s. c o m
public static void runUiDelayed(final Runnable runnable) {
    (new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... arg0) {
            return null;
        }

        /**
         * Always invoked on UI thread.
         */
        @Override
        protected void onPostExecute(Void result) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(runnable);
        }
    }).execute();
}

From source file:Main.java

public static void runAsync(final Runnable r) {
    new AsyncTask<Void, Void, Void>() {
        @Override//from w  w w .j  a v a2 s. c  o  m
        protected Void doInBackground(Void... unused) {
            r.run();
            return null;
        }
    }.execute();
}

From source file:Main.java

public static void jumpPostToActivity(final Context context, final Class<? extends Activity> targetClass,
        final int second) {
    (new AsyncTask<String, Integer, Boolean>() {
        protected Boolean doInBackground(String... params) {
            try {
                Thread.sleep((long) (second * 1000));
            } catch (Exception var3) {
                ;/*from  w w w. j a  v a  2s.c om*/
            }
            return null;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Intent datatIntent = new Intent(context, targetClass);
            context.startActivity(datatIntent);
        }
    }).execute(new String[] { "" });
}

From source file:cloud4all.Utils.NetworkAnode.java

public static void sendRequest(String anode) {
    AsyncTask<String, Void, Void> at = new AsyncTask<String, Void, Void>() {
        @Override//from ww  w.j ava2s.  co m
        protected Void doInBackground(String... url) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet command = new HttpGet(url[0]);
                HttpResponse response = client.execute(command);

                InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
                BufferedReader in = new BufferedReader(isr);
                String res = in.readLine();
                Log.d("GPIIUserListeners", "GPII has returned: " + res);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    at.execute(anode);
}

From source file:Main.java

public static void jumpPostToNewTopActivity(final Context context, final Class<? extends Activity> targetClass,
        final int second, final String... datas) {
    (new AsyncTask<String, Integer, Boolean>() {
        protected Boolean doInBackground(String... params) {
            try {
                Thread.sleep((long) (second * 1000));
            } catch (Exception var3) {
                ;/*ww w .ja  va 2  s.  co  m*/
            }
            return null;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Intent datatIntent = new Intent(context, targetClass);
            datatIntent.setFlags(335544320);
            if (datas != null) {
                for (int i = 0; i < datas.length; ++i) {
                    datatIntent.putExtra("data" + i, datas[i]);
                }
            }

            context.startActivity(datatIntent);
        }
    }).execute(new String[] { "" });
}

From source file:Main.java

public static void jumpPostToNewActivity(final Context context, final Class<? extends Activity> targetClass,
        final int second, final String... datas) {
    (new AsyncTask<String, Integer, Boolean>() {
        protected Boolean doInBackground(String... params) {
            try {
                Thread.sleep((long) (second * 1000));
            } catch (Exception var3) {
                ;//from  www. ja v  a  2  s  .c o  m
            }
            return null;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Intent datatIntent = new Intent(context, targetClass);
            datatIntent.setFlags(268435456);
            if (datas != null) {
                for (int i = 0; i < datas.length; ++i) {
                    datatIntent.putExtra("data" + i, datas[i]);
                }
            }

            context.startActivity(datatIntent);
        }
    }).execute(new String[] { "" });
}

From source file:mc.lib.network.AsyncNetworkHelper.java

public static void hasConnection(final Context context, final OnCompleteListener<Boolean> listener) {
    new AsyncTask<Void, Void, Boolean>() {
        @Override//from  w ww  .j  a v  a2 s . c om
        protected Boolean doInBackground(Void... params) {
            return NetworkHelper.hasConnection(context);
        }

        @Override
        protected void onPostExecute(Boolean res) {
            super.onPostExecute(res);
            listener.complete(res);
        }
    }.execute((Void) null);
}

From source file:Main.java

public static void setBreathingBackgroundColor(final View view, final int color) {
    Date firstDate = new Date();
    final long firstTime = firstDate.getTime();
    mAsyncTask = new AsyncTask<Void, Integer, Void>() {
        int n = 1, t = 3000;
        boolean increaseN;

        @Override/*  w w  w.  ja v  a2 s .com*/
        protected Void doInBackground(Void... params) {
            while (!isCancelled() || !mCancelled) {
                Date currentDate = new Date();
                long diffTime = currentDate.getTime() - firstTime;
                if (diffTime > n * t) {
                    increaseN = true;
                }
                if (increaseN) {
                    n++;
                    increaseN = false;
                }
                double y = getBreathingY(diffTime, n, t);
                int alpha = (int) ((y * 0.618f + 0.382f) * 255);
                int resultColor = setAlphaComponent(color, alpha);
                mColor = resultColor;
                publishProgress(resultColor);
                try {
                    Thread.sleep(38);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            view.setBackgroundColor(values[0]);
        }
    };
    executeAsyncTask(mAsyncTask);
}