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 fixAsyncTaskBug() {
    new AsyncTask<Void, Void, Void>() {

        @Override/*from   w  ww .j  a  v a 2 s .com*/
        protected Void doInBackground(Void... params) {
            return null;
        }
    }.execute();
}

From source file:Main.java

public static void run(final Runnable run) {
    new AsyncTask<Void, Void, Void>() {

        @Override//  ww w  . j  a va2s.  c  o  m
        protected Void doInBackground(Void... params) {
            run.run();
            return null;
        }
    };
}

From source file:Main.java

public static void doInAsync(final Runnable runnable) {
    new AsyncTask<Void, Void, Void>() {

        @Override//from  w ww  .jav  a2  s  . com
        protected Void doInBackground(Void... params) {
            return null;
        }

        protected void onPostExecute(Void result) {
            runnable.run();
        }

    }.execute();
}

From source file:Main.java

public static void Run(Runnable r) {
    new AsyncTask<Void, Void, Void>() {
        @Override/*from   ww  w .j a  va2s .  co  m*/
        protected Void doInBackground(Void... params) {
            r.run();
            return null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:Main.java

public static void killPackage(final String packageToKill) {
    new AsyncTask<Void, Void, Void>() {
        @Override//  w  w  w . ja  va  2 s.c o m
        protected Void doInBackground(Void... params) {
            try {
                Runtime.getRuntime().exec(new String[] { "su", "-c", "pkill " + packageToKill });
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
}

From source file:Main.java

public static void jumpPostToActivity(final Context context, final Intent datatIntent, 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.ja va  2 s  .c  o m*/
            return null;
        }

        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            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) {
    (new AsyncTask<String, Integer, Boolean>() {
        protected Boolean doInBackground(String... params) {
            try {
                Thread.sleep((long) (second * 1000));
            } catch (Exception var3) {
                ;/* w  w  w . j a 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);
            context.startActivity(datatIntent);
        }
    }).execute(new String[] { "" });
}

From source file:Main.java

public static void mediaScannerCall(final Context context, final File file) {
    new AsyncTask<Void, Void, Void>() {
        @Override//from   ww w . j  a  v a2  s  .co m
        protected Void doInBackground(Void... params) {
            MediaScannerConnection.scanFile(context, new String[] { file.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });
            return null;
        }
    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
}

From source file:Main.java

public static void jumpPostToNewTopActivity(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  a2s .c o m

            return null;
        }

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

From source file:Main.java

private static void smoothToOrigin(final View view) {
    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  o m*/
        protected Void doInBackground(Void... params) {
            while (!isCancelled()) {
                Date currentDate = new Date();
                long diffTime = currentDate.getTime() - firstTime;

                double y = getCosY(diffTime);
                int alpha = (int) (y * 255);
                int resultColor = setAlphaComponent(mColor, 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]);
        }
    });
}