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:info.staticfree.mqtt_camera.fragment.CameraFragment.java

/**
 * Shows a {@link Toast} on the UI thread.
 *
 * @param text The message to show//from   ww w. j  a  v  a 2s.  co m
 */
private void showToast(String text) {
    Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(() -> Toast.makeText(activity, text, Toast.LENGTH_SHORT).show());
    }
}

From source file:io.selendroid.server.model.DefaultSelendroidDriver.java

public void rotate(final ScreenOrientation orientation) {
    final Activity activity = serverInstrumentation.getCurrentActivity();
    if (activity == null) {
        return;/*from  ww  w .j  a v  a2  s  . c o m*/
    }
    final int screenOrientation = getAndroidScreenOrientation(orientation);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            activity.setRequestedOrientation(screenOrientation);
        }
    });
    serverInstrumentation.waitForIdleSync();
}

From source file:cliq.com.cliqgram.fragments.CameraFragment.java

/**
 * Shows a {@link Toast} on the UI thread.
 *
 * @param text The message to show//from   w  ww. j a v  a  2 s.c  o  m
 */
private void showToast(final String text) {
    final Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_LONG).show();
            }
        });
    }
}

From source file:com.filemanager.free.utils.MainActivityHelper.java

public void rename(int mode, String f, String f1, final Activity context, boolean rootmode) {
    final Toast toast = Toast.makeText(context, R.string.renaming, Toast.LENGTH_LONG);
    toast.show();/*from   w ww .j  av a 2  s .  c  om*/
    Operations.rename(new HFile(mode, f), new HFile(mode, f1), rootmode, context,
            new Operations.ErrorCallBack() {
                @Override
                public void exists(HFile file) {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (toast != null)
                                toast.cancel();
                            Toast.makeText(mainActivity, (R.string.fileexist), Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void launchSAF(HFile file) {

                }

                @Override
                public void launchSAF(final HFile file, final HFile file1) {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (toast != null)
                                toast.cancel();
                            mainActivity.oppathe = file.getPath();
                            mainActivity.oppathe1 = file1.getPath();
                            mainActivity.operation = DataUtils.RENAME;
                            guideDialogForLEXA(mainActivity.oppathe1);
                        }
                    });
                }

                @Override
                public void done(HFile hFile, final boolean b) {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (toast != null)
                                toast.cancel();
                            if (b) {
                                Intent intent = new Intent("loadlist");
                                mainActivity.sendBroadcast(intent);
                            } else
                                Toast.makeText(context, R.string.operationunsuccesful, Toast.LENGTH_SHORT)
                                        .show();

                        }
                    });
                }
            });
}

From source file:org.deviceconnect.android.deviceplugin.theta.fragment.ThetaVRModeFragment.java

/**
 * Save ScreenShot.//w  ww .  ja v  a 2  s  .  co  m
 */
private void saveScreenShot() {
    FileManager fileManager = new FileManager(getActivity());
    fileManager.checkWritePermission(new FileManager.CheckPermissionCallback() {
        @Override
        public void onSuccess() {
            Activity activity = getActivity();
            if (activity != null && !ThetaObjectStorage.hasEnoughStorageSize()) {
                if (mProgress != null) {
                    mProgress.dismiss();
                    mProgress = null;
                }
                // Check Android Storage Limit
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ThetaDialogFragment.showAlert(getActivity(),
                                getResources().getString(R.string.theta_ssid_prefix),
                                getResources().getString(R.string.theta_error_shortage_by_android), null);
                    }
                });
                return;
            }
            String root = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/";
            File dir = new File(root);
            if (!dir.exists()) {
                dir.mkdir();
            }

            Date date = new Date();
            SimpleDateFormat fileDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
            final String fileName = "theta_vr_screenshot_" + fileDate.format(date) + ".jpg";
            final String filePath = root + fileName;

            try {
                saveFile(filePath, mSphereView.takeSnapshot());
                if (BuildConfig.DEBUG) {
                    mLogger.severe("absolute path:" + filePath);
                }
                ContentValues values = new ContentValues();
                ContentResolver contentResolver = getActivity().getContentResolver();
                values.put(MediaStore.Images.Media.TITLE, fileName);
                values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
                values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
                values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                values.put(MediaStore.Images.Media.DATA, filePath);
                contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                if (activity != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ThetaDialogFragment.showAlert(getActivity(),
                                    getResources().getString(R.string.theta_ssid_prefix),
                                    getResources().getString(R.string.theta_save_screenshot), null);
                        }
                    });
                }
            } catch (IOException e) {
                if (activity != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            failSaveDialog();
                        }
                    });
                }
            } finally {
                if (activity != null) {
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mProgress != null) {
                                mProgress.dismiss();
                            }
                        }
                    });
                }
            }

        }

        @Override
        public void onFail() {
            Activity activity = getActivity();
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mProgress != null) {
                            mProgress.dismiss();
                        }
                        failSaveDialog();
                    }
                });
            }
        }
    });

}

From source file:camera2basic.Camera2BasicFragment.java

/**
 * Shows a {@link Toast} on the UI thread.
 *
 * @param text The message to show//  w  w  w .  j  a v  a  2  s.c  om
 */
private void showToast(final String text) {
    final Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

From source file:com.manateeworks.barcodescanner.MwbarcodescannerModule.java

@Kroll.method
public void startScanning(KrollDict options) {
    Log.d(TAG, "start scanning");

    final KrollFunction successCallback = getCallback(options, "success");
    final KrollFunction cancelCallback = getCallback(options, "cancel");
    final KrollFunction errorCallback = getCallback(options, "error");

    final Activity activity = TiApplication.getAppCurrentActivity();
    final TiActivitySupport activitySupport = (TiActivitySupport) activity;

    final TiIntentWrapper barcodeIntent = new TiIntentWrapper(new Intent(activity, ScannerActivity.class));
    barcodeIntent.setWindowId(TiIntentWrapper.createActivityName("SCANNER"));

    BarcodeResultHandler resultHandler = new BarcodeResultHandler();

    resultHandler.successCallback = successCallback;
    resultHandler.cancelCallback = cancelCallback;
    resultHandler.errorCallback = errorCallback;
    resultHandler.activitySupport = activitySupport;
    resultHandler.barcodeIntent = barcodeIntent.getIntent();

    activity.runOnUiThread(resultHandler);

    // activity.runOnUiThread(resultHandler);
    // activity.startActivity(barcodeIntent);

    // activity.runOnUiThread(new tia);

}

From source file:de.manumaticx.crouton.Manager.java

/**
 * Adds a {@link Crouton} to the {@link ViewParent} of it's {@link Activity}.
 *
 * @param crouton// w w  w .  ja  v a  2  s. com
 *     The {@link Crouton} that should be added.
 */
private void addCroutonToView(final Crouton crouton) {
    // don't add if it is already showing
    if (crouton.isShowing()) {
        return;
    }

    final View croutonView = crouton.getView();
    if (null == croutonView.getParent()) {
        ViewGroup.LayoutParams params = croutonView.getLayoutParams();
        if (null == params) {
            params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        // display Crouton in ViewGroup is it has been supplied
        if (null != crouton.getViewGroup()) {
            // TODO implement add to last position feature (need to align with how this will be requested for activity)
            if (crouton.getViewGroup() instanceof FrameLayout) {
                crouton.getViewGroup().addView(croutonView, params);
            } else {
                crouton.getViewGroup().addView(croutonView, 0, params);
            }
        } else {
            Activity activity = crouton.getActivity();
            if (null == activity || activity.isFinishing()) {
                return;
            }
            handleTranslucentActionBar((ViewGroup.MarginLayoutParams) params, activity);

            if (Looper.myLooper() == Looper.getMainLooper()) {
                activity.addContentView(croutonView, params);
            } else {
                final Activity mActivity = activity;
                final ViewGroup.LayoutParams mParams = params;
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mActivity.addContentView(croutonView, mParams);
                    }
                });
            }
        }
    }

    croutonView.requestLayout(); // This is needed so the animation can use the measured with/height
    ViewTreeObserver observer = croutonView.getViewTreeObserver();
    if (null != observer) {
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            @TargetApi(16)
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    croutonView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    croutonView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                croutonView.startAnimation(crouton.getInAnimation());
                announceForAccessibilityCompat(crouton.getActivity(), crouton.getText());
                if (Configuration.DURATION_INFINITE != crouton.getConfiguration().durationInMilliseconds) {
                    sendMessageDelayed(crouton, Messages.REMOVE_CROUTON,
                            crouton.getConfiguration().durationInMilliseconds
                                    + crouton.getInAnimation().getDuration());
                }
            }
        });
    }
}

From source file:im.neon.activity.CommonActivityUtils.java

/**
 * Helper method to display a toast message.
 *
 * @param aCallingActivity calling Activity instance
 * @param aMsgToDisplay    message to display
 */// www.j  ava 2s.  com
public static void displayToastOnUiThread(final Activity aCallingActivity, final String aMsgToDisplay) {
    if (null != aCallingActivity) {
        aCallingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                CommonActivityUtils.displayToast(aCallingActivity.getApplicationContext(), aMsgToDisplay);
            }
        });
    }
}