Example usage for android.os Handler post

List of usage examples for android.os Handler post

Introduction

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

Prototype

public final boolean post(Runnable r) 

Source Link

Document

Causes the Runnable r to be added to the message queue.

Usage

From source file:com.entropy.promoenginedemoapp.gcm.MyGcmListenerService.java

/**
 * Called when message is received./* ww  w.j av  a2s .c  o  m*/
 *
 * @param from SenderID of the sender.
 * @param data Data bundle containing message data as key/value pairs.
 *             For Set of keys use data.keySet().
 */
// [START receive_message]
@Override
public void onMessageReceived(String from, final Bundle data) {
    final String message = data.getString("message");
    //        Log.d(TAG, "From: " + from);
    //        Log.d(TAG, "Message: " + message);
    //        Log.d(TAG, data.toString());

    Handler mHandler = new Handler(getMainLooper());
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            SecondActivity act = new SecondActivity();
            act.redemtionForPromo(data);
        }
    });

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }

    // [START_EXCLUDE]
    /**
     * Production applications would usually process the message here.
     * Eg: - Syncing with server.
     *     - Store message in local database.
     *     - Update UI.
     */

    /**
     * In some cases it may be useful to show a notification indicating to the user
     * that a message was received.
     */

    if (isApplicationSentToBackground(getApplicationContext())) {
        sendNotification(message);
    } else {
        postMessage(message);
    }

    // [END_EXCLUDE]
}

From source file:com.miz.service.IdentifyMovieService.java

@Override
protected void onHandleIntent(Intent intent) {

    if (MizLib.isMovieLibraryBeingUpdated(this)) {
        Handler mHandler = new Handler(Looper.getMainLooper());
        mHandler.post(new Runnable() {
            @Override/*from ww  w.j  a va 2s .  c  o  m*/
            public void run() {
                Toast.makeText(IdentifyMovieService.this, R.string.cant_identify_while_updating,
                        Toast.LENGTH_LONG).show();
            }
        });

        return;
    }

    log("clear()");
    clear();

    log("setup()");
    setup();

    log("Intent extras");
    Bundle b = intent.getExtras();
    mMovieId = b.getString("movieId");
    mLanguage = b.getString("language", "en");
    mFilepath = b.getString("filepath");
    mOldMovieId = b.getString("currentMovieId");

    log("setupList()");
    setupList();

    log("start()");
    start();
}

From source file:com.massivcode.weatherinfoexam.fragment.ForecastFragment.java

public void setData(List<String> groupList, List<List<ForecastEachInfo>> childList) {
    mGroupData = groupList;//ww  w.  j av a 2  s .co  m
    mChildData = childList;

    Log.d(TAG, "? : " + childList);

    if (mAdapter == null) {
        mAdapter = new ExpAdapter(mGroupData, mChildData, getActivity());
    } else {
        mAdapter.notifyDataSetChanged();
    }

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            mListView.setAdapter(mAdapter);
        }
    });

}

From source file:com.campusconnect.gcm.GcmIntentService.java

@Override
protected final void onHandleIntent(final Intent intent) {
    Bundle extras = intent.getExtras();/* www. j  a v  a 2s. com*/
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) { // has effect of unpacking Bundle
        /*
         * Filter messages based on message type. Since it is likely that
         * GCM will be extended in the future with new message types,
         * just ignore any message types you're not interested in, or that
         * you don't recognize.
         */
        switch (messageType) {
        case GoogleCloudMessaging.MESSAGE_TYPE_DELETED:
            sendNotification("Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message, do some work.
            break;
        case GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE:
            if (intent.getStringExtra("NotificationKind").equals("PriceCheckLowerPrices1")) {
                final String message = getUserMessageForPriceCheckLowerPricesNotif(intent);

                Handler h = new Handler(Looper.getMainLooper());
                h.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
            }
            break;
        case GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR:
        default:
            sendNotification("Send error: " + extras.toString());
            break;
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

From source file:io.relayr.tellmewhen.gcm.GcmIntentService.java

private void playSound() {
    if (playingSound)
        return;/*from   ww w  .j a  va 2  s .  co m*/

    playingSound = true;

    Handler mHandler = new Handler(getMainLooper());
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            RingtoneManager.getRingtone(getApplicationContext(), sound).play();
            playingSound = false;
        }
    });
}

From source file:com.owncloud.android.providers.UsersAndGroupsSearchProvider.java

/**
 * Show error message//from   w  w w .j a  v a2  s  .c  o m
 *
 * @param result    Result with the failure information.
 */
public void showErrorMessage(final RemoteOperationResult result) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            // The Toast must be shown in the main thread to grant that will be hidden correctly; otherwise
            // the thread may die before, an exception will occur, and the message will be left on the screen
            // until the app dies
            Toast.makeText(getContext().getApplicationContext(),
                    ErrorMessageAdapter.getErrorCauseMessage(result, null, getContext().getResources()),
                    Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:com.echopf.members.ECHOMemberQuery.java

/**
 * Does Logout/*from  www  .  j  a  v  a2s  .  c  o m*/
 * @param sync if set TRUE, then the main (UI) thread is waited for complete the logging-out in a background thread. 
 *              (a synchronous communication)
 * @param callback invoked after the logging-out is completed
 * @param instanceId the reference ID of the instance to which the logged-out member belong
 * @throws ECHOException
 */
protected static void doLogout(final boolean sync, final ResultCallback callback, final String instanceId)
        throws ECHOException {
    final Handler handler = new Handler();

    // Get ready a background thread
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Callable<Void> communicator = new Callable<Void>() {

        @Override
        public Void call() throws ECHOException {

            ECHOException exception = null;

            try {
                ECHOQuery.postRequest(instanceId + "/login", new JSONObject());
            } catch (ECHOException e) {
                exception = e;
            } catch (Exception e) {
                exception = new ECHOException(e);
            }

            if (sync == false) {

                // Execute a callback method in the main (UI) thread.
                if (callback != null) {
                    final ECHOException fException = exception;

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.done(fException);
                        }
                    });
                }

                return null;

            } else {

                if (exception == null)
                    return null;
                throw exception;
            }
        }
    };

    Future<Void> future = executor.submit(communicator);

    if (sync) {
        try {
            future.get();
            return;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // ignore/reset
        } catch (ExecutionException e) {
            Throwable e2 = e.getCause();

            if (e2 instanceof ECHOException) {
                throw (ECHOException) e2;
            }

            throw new RuntimeException(e2);
        }
    }

    return;
}

From source file:com.android.screenspeak.contextmenu.ListMenuManager.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED && mDeferredAction != null) {
        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override/*from  w  w w . j  a va  2 s.co  m*/
            public void run() {
                if (mDeferredAction != null) {
                    mDeferredAction.run();
                    mDeferredAction = null;
                }
            }
        });
        mService.postRemoveEventListener(this);
    }
}

From source file:com.school.mailclient.app.fragment.SentFragment.java

public void updateList(final ArrayList<Mail> list) {
    Handler refresh = new Handler(Looper.getMainLooper());
    refresh.post(new Runnable() {
        public void run() {
            if (list != null) {
                adapter = new MailAdapter(getActivity(), list);
                sentList.setAdapter(adapter);
            }/*from   w ww.  j av  a  2  s  .com*/
        }
    });
}

From source file:com.school.mailclient.app.fragment.InboxFragment.java

public void updateList(final ArrayList<Mail> list) {
    Handler refresh = new Handler(Looper.getMainLooper());
    refresh.post(new Runnable() {
        public void run() {
            if (list != null) {
                adapter = new MailAdapter(getActivity(), list);
                inboxList.setAdapter(adapter);
            }/*from  w  ww .  jav a 2s  .c  o m*/
        }
    });
}