Example usage for android.app Activity runOnUiThread

List of usage examples for android.app Activity runOnUiThread

Introduction

In this page you can find the example usage for android.app Activity runOnUiThread.

Prototype

public final void runOnUiThread(Runnable action) 

Source Link

Document

Runs the specified action on the UI thread.

Usage

From source file:Main.java

public static void showAlertDialog(final Activity activity, final String titel, final String text) {

    if (!activity.isFinishing()) {
        activity.runOnUiThread(new Runnable() {

            public void run() {
                aDialogBuilder = new AlertDialog.Builder(activity);
                aDialogBuilder.setMessage(text);
                aDialogBuilder.setTitle(titel);
                aDialogBuilder.setCancelable(false);
                aDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                    }// w ww. ja v  a2 s.  c om
                });
                aDialog = aDialogBuilder.create();
                aDialog.show();
            }
        });
    }

}

From source file:Main.java

public static void dismissProgressDialog(Activity context) {
    if (context == null || progressDialog == null || progressDialog.isShowing() == false) {
        return;// ww  w  . jav a  2s  .c om
    }
    context.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.dismiss();
        }
    });
}

From source file:Main.java

public static View selectAndFindListViewChildAt(final Activity activity, final ListView listView,
        final int position, final long timeout) throws InterruptedException {
    activity.runOnUiThread(new Runnable() {
        public void run() {
            listView.setSelection(position);
        }// w  ww  .  j a  v  a2 s. co m
    });
    long start = System.currentTimeMillis();
    View child = null;
    while (listView.getLastVisiblePosition() < position || child == null) {
        Thread.sleep(50);
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        child = listView.getChildAt(position - firstVisiblePosition);
        long now = System.currentTimeMillis();
        if (now - start > timeout) {
            return null;
        }
    }
    return child;
}

From source file:Main.java

public static void ToastMsg(final Activity activity, final String msg) {
    if (activity == null || TextUtils.isEmpty(msg)) {
        return;/*from w  w w  .ja  v a2  s. co  m*/
    }
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (mToast != null) {
                mToast.cancel();
                mToast = null;
            }
            mToast = Toast.makeText(activity, msg, Toast.LENGTH_SHORT);
            mToast.show();
        }
    });
}

From source file:Main.java

public static void showToast(final Activity context, final String msg) {
    if ("main".equals(Thread.currentThread().getName())) {
        Toast.makeText(context, msg, 1).show();
    } else {//from  w  w w. j  av a  2 s  . c  o m
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg, 1).show();
            }
        });
    }
}

From source file:Main.java

public static void showToast(final Activity activity, final String msg) {
    Log.i(TAG, "Show Toast: " + msg);

    if (activity == null) {
        return;// www  .ja v  a 2 s . co  m
    }
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                Toast toast = Toast.makeText(activity, msg, Toast.LENGTH_LONG);
                toast.show();
            } catch (Throwable e) {
                Log.e(TAG, "error: " + e, e);
            }
        }
    });
}

From source file:Main.java

/**
 * Save a media in the downloads directory and offer to open it with a third party application.
 * @param activity the activity//from w w  w .j  a va2  s.  c  o  m
 * @param savedMediaPath the media path
 * @param mimeType the media mime type.
 */
public static void openMedia(final Activity activity, final String savedMediaPath, final String mimeType) {
    if ((null != activity) && (null != savedMediaPath)) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    File file = new File(savedMediaPath);
                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), mimeType);
                    activity.startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(activity, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                }
            }
        });
    }
}

From source file:Main.java

public static void performClickOnUIThread(final Activity activity, final View item) {
    Runnable click = new Runnable() {
        public void run() {
            item.performClick();// w  ww  . jav a 2 s . c o m
        }
    };
    activity.runOnUiThread(click);
}

From source file:Main.java

public static void performDelayedInUiThread(final Activity activity, final Runnable task, int delay) {
    new Timer().schedule(new TimerTask() {
        @Override/*w ww  .j  a v  a 2  s  .  com*/
        public void run() {
            activity.runOnUiThread(task);
        }
    }, delay);
}

From source file:Main.java

public static void performLongClickOnUIThread(final Activity activity, final View item) {
    Runnable click = new Runnable() {
        public void run() {
            item.performLongClick();//from www. j  a  v a  2 s  . c o  m
        }
    };
    activity.runOnUiThread(click);
}