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:github.daneren2005.dsub.util.Notifications.java

public static void showDownloadingNotification(final Context context, final DownloadService downloadService,
        Handler handler, DownloadFile file, int size) {
    Intent cancelIntent = new Intent(context, DownloadService.class);
    cancelIntent.setAction(DownloadService.CANCEL_DOWNLOADS);
    PendingIntent cancelPI = PendingIntent.getService(context, 0, cancelIntent, 0);

    String currentDownloading, currentSize;
    if (file != null) {
        currentDownloading = file.getSong().getTitle();
        currentSize = Util.formatLocalizedBytes(file.getEstimatedSize(), context);
    } else {/*from  w  ww  .j  a  va 2 s  .  c o  m*/
        currentDownloading = "none";
        currentSize = "0";
    }

    NotificationCompat.Builder builder;
    builder = new NotificationCompat.Builder(context).setSmallIcon(android.R.drawable.stat_sys_download)
            .setContentTitle(context.getResources().getString(R.string.download_downloading_title, size))
            .setContentText(
                    context.getResources().getString(R.string.download_downloading_summary, currentDownloading))
            .setStyle(new NotificationCompat.BigTextStyle().bigText(context.getResources().getString(
                    R.string.download_downloading_summary_expanded, currentDownloading, currentSize)))
            .setProgress(10, 5, true).setOngoing(true).addAction(R.drawable.notification_close,
                    context.getResources().getString(R.string.common_cancel), cancelPI);

    Intent notificationIntent = new Intent(context, SubsonicFragmentActivity.class);
    notificationIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD_VIEW, true);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    builder.setContentIntent(PendingIntent.getActivity(context, 2, notificationIntent, 0));

    final Notification notification = builder.build();
    downloadShowing = true;
    if (playShowing) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID_DOWNLOADING, notification);
    } else {
        downloadForeground = true;
        handler.post(new Runnable() {
            @Override
            public void run() {
                downloadService.startForeground(NOTIFICATION_ID_DOWNLOADING, notification);
            }
        });
    }

}

From source file:com.ifeng.util.download.DownloadThread.java

/**
 * ??/*from  w w w  .j  a  v a2 s  .  c om*/
 * 
 * @param status
 *            ?? Downloads.status_*
 * @param msg
 *            ?
 */
private void displayMsg(final int status, final String msg) {
    // ??
    // Intent intent = new Intent();
    // intent.setClass(mContext, DownloadMsgActivity.class);
    // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // intent.putExtra(DownloadMsgActivity.EXTRA_STATUS, status);
    // intent.putExtra(DownloadMsgActivity.EXTRA_MSG, msg);
    //
    // mContext.startActivity(intent);
    // ????
    if (mInfo.mVisibility == Downloads.Impl.VISIBILITY_HIDDEN) {
        return;
    }
    Handler handler = new Handler(mContext.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            showMsg(status, msg);
        }
    });
}

From source file:com.garrapeta.jumplings.billing.util.IabHelper.java

void consumeAsyncInternal(final List<Purchase> purchases, final OnConsumeFinishedListener singleListener,
        final OnConsumeMultiFinishedListener multiListener) {
    final Handler handler = new Handler();
    flagStartAsync("consume");
    (new Thread(new Runnable() {
        @Override//from w w w. ja v a 2  s. co m
        public void run() {
            final List<IabResult> results = new ArrayList<IabResult>();
            for (Purchase purchase : purchases) {
                try {
                    consume(purchase);
                    results.add(new IabResult(BILLING_RESPONSE_RESULT_OK,
                            "Successful consume of sku " + purchase.getSku()));

                    if (singleListener != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                singleListener.onConsumeFinished(purchases.get(0), results.get(0));
                            }
                        });
                    }
                    if (multiListener != null) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                multiListener.onConsumeMultiFinished(purchases, results);
                            }
                        });
                    }
                } catch (IabException ex) {
                    results.add(ex.getResult());
                } finally {
                    flagEndAsync();
                }
            }

        }
    })).start();
}

From source file:com.cdvdev.subscriptiondemo.helpers.IabHelper.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread./*  w w w. ja va2 s .  co m*/
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");

    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                    }
                });
            }
        }
    })).start();
}

From source file:org.kontalk.billing.GoogleBillingService.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread.//from  www . ja  v  a 2 s.  c om
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkNotDisposed();
    checkSetupDone("queryInventory");
    startAsyncOperation("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            BillingResult result = new BillingResult(BILLING_RESPONSE_RESULT_OK,
                    "Inventory refresh successful.");
            IInventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (BillingException ex) {
                result = ex.getResult();
            }

            endAsyncOperation();

            final BillingResult result_f = result;
            final IInventory inv_f = inv;
            if (!mDisposed && listener != null) {
                handler.post(new Runnable() {
                    public void run() {
                        listener.onQueryInventoryFinished(result_f, inv_f);
                    }
                });
            }
        }
    })).start();
}

From source file:com.abplus.surroundcalc.billing.BillingHelper.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread./*from w  w w  . ja  v  a  2 s. co m*/
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkSetupDone("queryInventory");
    asyncFlag.start("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            Result result = new Result(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (BillingException ex) {
                result = ex.getResult();
            }

            asyncFlag.end();

            final Result result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    listener.onQueryInventoryFinished(result_f, inv_f);
                }
            });
        }
    })).start();
}

From source file:com.facebook.notifications.NotificationsManager.java

/**
 * Prepare and pre-load a notification bundle into memory.
 *
 * @param context            The current context of your program. Usually an activity, application, or
 *                           service context.
 * @param notificationBundle The bundle containing the notification payload to present
 * @param callback           The callback to invoke once preparation is complete. This is guaranteed to be
 *                           invoked on the same thread as this method is invoked from.
 *//*from  www. j  a v a2 s.c  om*/
public static void prepareCard(@NonNull final Context context, @NonNull final Bundle notificationBundle,
        @NonNull final PrepareCallback callback) {
    final Handler handler = new Handler();
    final AssetManager assetManager = getAssetManager(context);
    final ContentManager contentManager = getContentManager(context);

    // Cache and prepare in background.
    new Thread() {
        @Override
        public void run() {
            try {
                JSONObject cardJSON = getCardJSON(notificationBundle);
                if (cardJSON == null) {
                    throw new NullPointerException("No content present in the notification bundle.");
                }
                Version cardVersion = Version.parse(cardJSON.optString("version"));
                if (cardVersion == null || cardVersion.compareTo(PAYLOAD_VERSION_OBJECT) > 0) {
                    throw new Exception("Payload version " + cardVersion
                            + " not supported by this version of the notifications SDK.");
                }

                assetManager.cachePayload(cardJSON, new AssetManager.CacheCompletionCallback() {
                    @Override
                    public void onCacheCompleted(@NonNull JSONObject payload) {
                        assetManager.stopCaching();

                        try {
                            JSONObject cardJSON = getCardJSON(notificationBundle);
                            final Intent presentIntent = intentForBundle(context,
                                    getPushJSON(notificationBundle), cardJSON, assetManager, contentManager);
                            if (presentIntent == null) {
                                throw new NullPointerException(
                                        "presentIntent was null, this should never happen!");
                            }

                            CardConfiguration configuration = new CardConfiguration(cardJSON, assetManager,
                                    contentManager);
                            presentIntent.putExtra(CardActivity.EXTRA_CONFIGURATION, configuration);

                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onPrepared(presentIntent);
                                }
                            });
                        } catch (final Exception ex) {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    callback.onError(ex);
                                }
                            });
                        }
                    }
                });
            } catch (final Exception ex) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onError(ex);
                    }
                });
            }
        }
    }.start();
}

From source file:co.hmsk.android.webdbpress76.util.IabHelper.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread./*from  ww  w  .ja v  a 2s  .c  o m*/
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    listener.onQueryInventoryFinished(result_f, inv_f);
                }
            });
        }
    })).start();
}

From source file:com.freshplanet.inapppurchase.IabHelper.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread./*  w  w w .  java  2s  . com*/
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param moreSubsSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final List<String> moreSubsSkus, final QueryInventoryFinishedListener listener) {
    logDebug("starting Query inventory async");
    final Handler handler = new Handler();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus, moreSubsSkus);
            } catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    listener.onQueryInventoryFinished(result_f, inv_f);
                }
            });
        }
    })).start();
}

From source file:com.photocontest.billing.IabHelper.java

/**
 * Asynchronous wrapper for inventory query. This will perform an inventory
 * query as described in {@link #queryInventory}, but will do so asynchronously
 * and call back the specified listener upon completion. This method is safe to
 * call from a UI thread.//from   w  w w.  ja  v a2  s  . c  o  m
 *
 * @param querySkuDetails as in {@link #queryInventory}
 * @param moreSkus as in {@link #queryInventory}
 * @param listener The listener to notify when the refresh operation completes.
 */
public void queryInventoryAsync(final boolean querySkuDetails, final List<String> moreSkus,
        final QueryInventoryFinishedListener listener) {
    final Handler handler = new Handler();
    checkSetupDone("queryInventory");
    flagStartAsync("refresh inventory");
    (new Thread(new Runnable() {
        public void run() {
            IabResult result = new IabResult(BILLING_RESPONSE_RESULT_OK, "Inventory refresh successful.");
            Inventory inv = null;
            try {
                inv = queryInventory(querySkuDetails, moreSkus);
            } catch (IabException ex) {
                result = ex.getResult();
            }

            flagEndAsync();

            final IabResult result_f = result;
            final Inventory inv_f = inv;
            handler.post(new Runnable() {
                public void run() {
                    listener.onQueryInventoryFinished(result_f, inv_f);

                }
            });
        }
    })).start();
}