Example usage for android.os Looper getMainLooper

List of usage examples for android.os Looper getMainLooper

Introduction

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

Prototype

public static Looper getMainLooper() 

Source Link

Document

Returns the application's main looper, which lives in the main thread of the application.

Usage

From source file:me.willowcheng.makerthings.core.OpenHABVoiceService.java

private void performHttpPost(final String itemName, final StringEntity command) {
    /* Call MyAsyncHttpClient on the main UI thread in order to retrieve the callbacks correctly.
     * If calling MyAsyncHttpClient directly, the following would happen:
     * (1) MyAsyncHttpClient performs the HTTP post asynchronously
     * (2) OpenHABVoiceService stops because all intents have been handled
     * (3) MyAsyncHttpClient tries to call onSuccess() or onFailure(), which is not possible
     *     anymore because OpenHABVoiceService is already stopped/destroyed.
     *//*from ww w  .j  a v  a  2 s . com*/
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            mAsyncHttpClient.post(OpenHABVoiceService.this, mOpenHABBaseUrl + "rest/items/" + itemName, command,
                    "text/plain;charset=UTF-8", new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                            Log.d(TAG, "Command was sent successfully");
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
                                Throwable error) {
                            Log.e(TAG, "Got command error " + statusCode, error);
                        }
                    });
        }
    });
}

From source file:com.grumpysailor.cordova.magnetsensor.MagnetSensor.java

/**
 * Start listening for acceleration sensor.
 * //  w ww .  j a  va 2 s.co m
 * @return          status of listener
*/
private int start() {
    // If already starting or running, then just return
    if ((this.status == MagnetSensor.RUNNING) || (this.status == MagnetSensor.STARTING)) {
        return this.status;
    }

    this.setStatus(MagnetSensor.STARTING);

    // Get accelerometer from sensor manager
    List<Sensor> list = this.mSensorManager.getSensorList(Sensor.TYPE_MAGNETIC_FIELD);

    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mMagnetometer = list.get(0);
        this.mSensorManager.registerListener(this, this.mMagnetometer, SensorManager.SENSOR_DELAY_UI);
        this.setStatus(MagnetSensor.STARTING);
    } else {
        this.setStatus(MagnetSensor.ERROR_FAILED_TO_START);
        this.fail(MagnetSensor.ERROR_FAILED_TO_START,
                "No sensors found to register magnet sensor listening to.");
        return this.status;
    }

    // Set a timeout callback on the main thread.
    stopTimeout();
    mainHandler = new Handler(Looper.getMainLooper());
    mainHandler.postDelayed(mainRunnable, 2000);

    return this.status;
}

From source file:org.quantumbadger.redreader.activities.CommentEditActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getTitle().equals(getString(R.string.comment_edit_save))) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(getString(R.string.comment_reply_submitting_title));
        progressDialog.setMessage(getString(R.string.comment_reply_submitting_message));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);

        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(final DialogInterface dialogInterface) {
                General.quickToast(CommentEditActivity.this, R.string.comment_reply_oncancel);
                progressDialog.dismiss();
            }/*from ww  w  .  j av  a 2 s.  com*/
        });

        progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(final DialogInterface dialogInterface, final int keyCode,
                    final KeyEvent keyEvent) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    General.quickToast(CommentEditActivity.this, R.string.comment_reply_oncancel);
                    progressDialog.dismiss();
                }

                return true;
            }
        });

        final APIResponseHandler.ActionResponseHandler handler = new APIResponseHandler.ActionResponseHandler(
                this) {
            @Override
            protected void onSuccess() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                        General.quickToast(CommentEditActivity.this, R.string.comment_edit_done);
                        finish();
                    }
                });
            }

            @Override
            protected void onCallbackException(Throwable t) {
                BugReportActivity.handleGlobalError(CommentEditActivity.this, t);
            }

            @Override
            protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                    String readableMessage) {

                final RRError error = General.getGeneralErrorForFailure(context, type, t, status, null);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentEditActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }

            @Override
            protected void onFailure(final APIFailureType type) {

                final RRError error = General.getGeneralErrorForFailure(context, type);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentEditActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }
        };

        final CacheManager cm = CacheManager.getInstance(this);
        final RedditAccount selectedAccount = RedditAccountManager.getInstance(this).getDefaultAccount();

        RedditAPI.editComment(cm, handler, selectedAccount, commentIdAndType, textEdit.getText().toString(),
                this);

        progressDialog.show();

    } else if (item.getTitle().equals(getString(R.string.comment_reply_preview))) {
        MarkdownPreviewDialog.newInstance(textEdit.getText().toString()).show(getSupportFragmentManager());
    }

    return true;
}

From source file:com.ryan.ryanreader.activities.CommentReplyActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getTitle().equals(getString(R.string.comment_reply_send))) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(getString(R.string.comment_reply_submitting_title));
        progressDialog.setMessage(getString(R.string.comment_reply_submitting_message));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);

        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(final DialogInterface dialogInterface) {
                General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
                progressDialog.dismiss();
            }//  w ww  .  ja va  2s.com
        });

        progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(final DialogInterface dialogInterface, final int keyCode,
                    final KeyEvent keyEvent) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
                    progressDialog.dismiss();
                }

                return true;
            }
        });

        final APIResponseHandler.ActionResponseHandler handler = new APIResponseHandler.ActionResponseHandler(
                this) {
            @Override
            protected void onSuccess() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                        General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_done));
                        finish();
                    }
                });
            }

            @Override
            protected void onCallbackException(Throwable t) {
                BugReportActivity.handleGlobalError(CommentReplyActivity.this, t);
            }

            @Override
            protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                    String readableMessage) {

                final RRError error = General.getGeneralErrorForFailure(context, type, t, status);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentReplyActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }

            @Override
            protected void onFailure(final APIFailureType type) {

                final RRError error = General.getGeneralErrorForFailure(context, type);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentReplyActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }
        };

        final CacheManager cm = CacheManager.getInstance(this);

        final ArrayList<RedditAccount> accounts = RedditAccountManager.getInstance(this).getAccounts();
        RedditAccount selectedAccount = null;

        for (RedditAccount account : accounts) {
            if (!account.isAnonymous()
                    && account.username.equalsIgnoreCase((String) usernameSpinner.getSelectedItem())) {
                selectedAccount = account;
                break;
            }
        }

        RedditAPI.comment(cm, handler, selectedAccount, parentIdAndType, textEdit.getText().toString(), this);

        progressDialog.show();

    } else if (item.getTitle().equals(getString(R.string.comment_reply_preview))) {
        MarkdownPreviewDialog.newInstance(textEdit.getText().toString()).show(getSupportFragmentManager());
    }

    return true;
}

From source file:org.quantumbadger.redreader.activities.CommentReplyActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getTitle().equals(getString(R.string.comment_reply_send))) {

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(getString(R.string.comment_reply_submitting_title));
        progressDialog.setMessage(getString(R.string.comment_reply_submitting_message));
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);

        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(final DialogInterface dialogInterface) {
                General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
                progressDialog.dismiss();
            }/*from   ww  w.j a  va2  s  .c o m*/
        });

        progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(final DialogInterface dialogInterface, final int keyCode,
                    final KeyEvent keyEvent) {

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_oncancel));
                    progressDialog.dismiss();
                }

                return true;
            }
        });

        final APIResponseHandler.ActionResponseHandler handler = new APIResponseHandler.ActionResponseHandler(
                this) {
            @Override
            protected void onSuccess() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                        General.quickToast(CommentReplyActivity.this, getString(R.string.comment_reply_done));
                        finish();
                    }
                });
            }

            @Override
            protected void onCallbackException(Throwable t) {
                BugReportActivity.handleGlobalError(CommentReplyActivity.this, t);
            }

            @Override
            protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                    String readableMessage) {

                final RRError error = General.getGeneralErrorForFailure(context, type, t, status, null);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentReplyActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }

            @Override
            protected void onFailure(final APIFailureType type) {

                final RRError error = General.getGeneralErrorForFailure(context, type);

                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    public void run() {
                        General.showResultDialog(CommentReplyActivity.this, error);
                        if (progressDialog.isShowing())
                            progressDialog.dismiss();
                    }
                });
            }
        };

        final CacheManager cm = CacheManager.getInstance(this);

        final ArrayList<RedditAccount> accounts = RedditAccountManager.getInstance(this).getAccounts();
        RedditAccount selectedAccount = null;

        for (RedditAccount account : accounts) {
            if (!account.isAnonymous()
                    && account.username.equalsIgnoreCase((String) usernameSpinner.getSelectedItem())) {
                selectedAccount = account;
                break;
            }
        }

        RedditAPI.comment(cm, handler, selectedAccount, parentIdAndType, textEdit.getText().toString(), this);

        progressDialog.show();

    } else if (item.getTitle().equals(getString(R.string.comment_reply_preview))) {
        MarkdownPreviewDialog.newInstance(textEdit.getText().toString()).show(getSupportFragmentManager());
    }

    return true;
}

From source file:com.kytse.aria2remote.DownloadListFragment.java

@Override
public void onAria2ListItemMoved(Aria2.ListType type, final int oldPosition, final int newPosition) {
    if (type == mType) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override//from   w w w. ja v a 2 s  .c  o m
            public void run() {
                mAdapter.notifyItemMoved(oldPosition, newPosition);
            }
        });
    }
}

From source file:com.applivery.applvsdklib.ui.views.update.MustUpdateViewImpl.java

public Looper getLooper() {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        return Looper.myLooper();
    } else {//w ww. j  a v  a2  s.c o  m
        return Looper.getMainLooper();
    }
}

From source file:com.facebook.internal.FacebookWebFallbackDialog.java

@Override
public void dismiss() {
    WebView webView = getWebView();/*from  w w w.j  a va 2s  .  com*/

    if (isListenerCalled() || webView == null || !webView.isShown()) {
        // If the listener has been called, or if the WebView isn't visible, we cannot give the dialog a chance
        // to respond. So defer to the parent implementation.
        super.dismiss();
        return;
    }

    // If we have already notified the dialog to close, then ignore this request to dismiss. The timer will
    // honor the request.
    if (waitingForDialogToClose) {
        return;
    }
    waitingForDialogToClose = true;

    // Now fire off the event that will tell the dialog to wind down.
    String eventJS = "(function() {" + "  var event = document.createEvent('Event');"
            + "  event.initEvent('fbPlatformDialogMustClose',true,true);" + "  document.dispatchEvent(event);"
            + "})();";
    webView.loadUrl("javascript:" + eventJS);

    // Set up a timeout for the dialog to respond. If the timer expires, we need to honor the user's desire to
    // dismiss the dialog.
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (!isListenerCalled()) {
                // If we get here, then the dialog did not close quickly enough. So we need to honor the user's
                // wish to cancel.
                sendCancelToListener();
            }
        }
    }, OS_BACK_BUTTON_RESPONSE_TIMEOUT_MILLISECONDS);
}

From source file:com.kaliturin.blacklist.utils.Utils.java

/**
 * Makes and shows threadsafe toast/*  w  w  w . j  a  v  a 2 s  . c  o m*/
 */
public static void showToast(final Context context, final String message, final int duration) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(context.getApplicationContext(), message, duration).show();
        }
    });
}

From source file:com.bsafe.sensors.motion.BSMotionSensorsPlugin.java

private void startTimeout() {
    // Set a timeout callback on the main thread.
    stopTimeout();/*w w w .  ja v  a  2  s.  c o  m*/
    mainHandler = new Handler(Looper.getMainLooper());
    mainHandler.postDelayed(mainRunnable, 2000);
}