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:it.mb.whatshare.Dialogs.java

/**
 * Shows a prompt to the user to rename the argument <tt>device</tt>.
 * //from w  w  w . j av a  2 s  .c  o m
 * @param device
 *            the device to be renamed
 * @param activity
 *            the parent activity
 */
public static void promptForNewDeviceName(final PairedDevice device, final MainActivity activity) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {

        @Override
        public void run() {
            DialogFragment prompt = new PatchedDialogFragment() {
                public Dialog onCreateDialog(Bundle savedInstanceState) {
                    AlertDialog.Builder builder = getBuilder(activity);

                    final EditText input = new EditText(getContext());
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                    input.setText(device.name);
                    input.setSelection(device.name.length());

                    // @formatter:off
                    builder.setTitle(R.string.device_name_chooser_title).setView(input)
                            .setPositiveButton(android.R.string.ok, null);
                    // @formatter:on

                    final AlertDialog alertDialog = builder.create();
                    alertDialog.setOnShowListener(
                            new DeviceNameChooserPrompt(alertDialog, input, activity, new Callback<String>() {

                                @Override
                                public void run() {
                                    // @formatter:off
                                    device.rename(getParam());
                                    activity.onSelectedDeviceRenamed();

                                    ((InputMethodManager) activity
                                            .getSystemService(Context.INPUT_METHOD_SERVICE))
                                                    .hideSoftInputFromWindow(input.getWindowToken(), 0);
                                    // @formatter:on
                                }
                            }));
                    return alertDialog;
                }
            };
            prompt.show(activity.getSupportFragmentManager(), "chooseName");
        }
    });
}

From source file:be.ac.ucl.lfsab1509.llncampus.UCLouvain.java

/**
 * Launch the download of the courses list and store them in the database.
 * //  ww w .ja  va  2s.  c o m
 * @param context
 *          Application context.
 * @param username
 *          UCL global user identifier.
 * @param password
 *          UCL password.
 * @param end
 *          Runnable to be executed at the end of the download.
 * @param mHandler
 *          Handler to manage messages and threads.
 *          
 */
public static void downloadCoursesFromUCLouvain(final LLNCampusActivity context, final String username,
        final String password, final Runnable end, final Handler mHandler) {
    Time t = new Time();
    t.setToNow();
    int year = t.year;
    // A new academic year begin in September (8th month on 0-based count).
    if (t.month < 8) {
        year--;
    }
    final int academicYear = year;

    mHandler.post(new Runnable() {

        public void run() {

            final ProgressDialog mProgress = new ProgressDialog(context);
            mProgress.setCancelable(false);
            mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgress.setMax(100);
            mProgress.setMessage(context.getString(R.string.connection));
            mProgress.show();

            new Thread(new Runnable() {
                /**
                 * Set the progress to the value n and show the message nextStep
                 * @param n
                 *          The progress value.
                 * @param nextStep
                 *          The message to show (indicate the next step to be processed).
                 */
                public void progress(final int n, final String nextStep) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProgress.setProgress(n);
                            mProgress.setMessage(nextStep);
                        }
                    });
                    Log.d("UCLouvain", nextStep);
                }

                /**
                 * Notify to the user an error.
                 * 
                 * @param msg
                 *          The message to show to the user.
                 */
                public void sendError(String msg) {
                    notify(context.getString(R.string.error) + " :" + msg);
                    mProgress.cancel();
                }

                /**
                 * Notify to the user a message.
                 * 
                 * @param msg
                 *          The message to show to the user.
                 */
                public void notify(final String msg) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            context.notify(msg);
                        }
                    });
                }

                public void run() {
                    progress(0, context.getString(R.string.connection_ucl));
                    UCLouvain uclouvain = new UCLouvain(username, password);

                    progress(20, context.getString(R.string.fetch_info));
                    ArrayList<Offer> offers = uclouvain.getOffers(academicYear);

                    if (offers == null || offers.isEmpty()) {
                        sendError(context.getString(R.string.error_academic_year) + academicYear);
                        return;
                    }

                    int i = 40;
                    ArrayList<Course> courses = new ArrayList<Course>();
                    for (Offer o : offers) {
                        progress(i, context.getString(R.string.get_courses) + o.getOfferName());
                        ArrayList<Course> offerCourses = uclouvain.getCourses(o);
                        if (offerCourses != null && !offerCourses.isEmpty()) {
                            courses.addAll(offerCourses);
                        } else {
                            Log.e("CoursListEditActivity", "Error : No course for offer [" + o.getOfferCode()
                                    + "] " + o.getOfferName());
                        }
                        i += (int) (30. / offers.size());
                    }

                    if (courses.isEmpty()) {
                        sendError(context.getString(R.string.courses_empty));
                        return;
                    }

                    // Remove old courses.
                    progress(70, context.getString(R.string.cleaning_db));
                    LLNCampus.getDatabase().delete("Courses", "", null);
                    LLNCampus.getDatabase().delete("Horaire", "", null);

                    // Add new data.
                    i = 80;
                    for (Course c : courses) {
                        progress(i, context.getString(R.string.add_courses_db));
                        ContentValues cv = new ContentValues();
                        cv.put("CODE", c.getCourseCode());
                        cv.put("NAME", c.getCoursName());

                        LLNCampus.getDatabase().insert("Courses", cv);
                        i += (int) (20. / courses.size());
                    }

                    progress(100, context.getString(R.string.end));
                    mProgress.cancel();
                    mHandler.post(end);
                }
            }).start();
        }
    });
}

From source file:net.ben.subsonic.androidapp.util.Util.java

public static void hidePlayingNotification(final Context context, Handler handler) {
    handler.post(new Runnable() {
        @Override/*w w  w.  j  a  va 2  s  . com*/
        public void run() {
            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(Constants.NOTIFICATION_ID_PLAYING);
        }
    });

    // Update widget
    DownloadService service = DownloadServiceImpl.getInstance();
    if (service != null) {
        SubsonicAppWidgetProvider.getInstance().notifyChange(context, service, false);
    }
}

From source file:com.dmbstream.android.util.Util.java

public static void hidePlayingNotification(final Context context, final DownloadServiceImpl downloadService,
        Handler handler) {

    // Remove notification and remove the service from the foreground
    handler.post(new Runnable() {
        @Override/*from   www. ja  v  a2  s  . c  o m*/
        public void run() {
            stopForeground(downloadService, true);
        }
    });

    // Update widget
    DmbstreamAppWidgetProvider.getInstance().notifyChange(context, downloadService, false);
}

From source file:emu.project64.GalleryActivity.java

public static void RecentRomsUpdated() {
    refreshRecentRoms();/*from   w  w w.  j  ava 2  s  .co m*/
    Handler h = new Handler(Looper.getMainLooper());
    h.post(new Runnable() {
        public void run() {
            mActiveGalleryActivity.refreshGrid();
        }
    });
}

From source file:emu.project64.GalleryActivity.java

public static void RomListReset() {
    mGalleryItems = new ArrayList<GalleryItem>();
    if (mActiveGalleryActivity != null && mActiveGalleryActivity.mProgress != null) {
        Handler h = new Handler(Looper.getMainLooper());
        h.post(new Runnable() {
            public void run() {
                mActiveGalleryActivity.mProgress.show();
            }// ww  w  .j  av  a  2s .  c  om
        });
    }
}

From source file:emu.project64.GalleryActivity.java

public static void RomListLoadDone() {
    refreshRecentRoms();//  w ww.  j a  v  a 2s. c  om
    if (mActiveGalleryActivity != null && mActiveGalleryActivity.mProgress != null) {
        Handler h = new Handler(Looper.getMainLooper());
        h.post(new Runnable() {
            public void run() {
                mActiveGalleryActivity.refreshGrid();
                mActiveGalleryActivity.mProgress.dismiss();
            }
        });
    }
}

From source file:net.tac42.subtails.util.Util.java

public static void hidePlayingNotification(final Context context, final DownloadServiceImpl downloadService,
        Handler handler) {

    // Remove notification and remove the service from the foreground
    handler.post(new Runnable() {
        @Override//from  w  ww . ja  v a  2  s . com
        public void run() {
            stopForeground(downloadService, true);
        }
    });

    // Update widget
    SubtailsAppWidgetProvider.getInstance().notifyChange(context, downloadService, false);
}

From source file:github.popeen.dsub.util.Notifications.java

public static void hideDownloadingNotification(final Context context, final DownloadService downloadService,
        Handler handler) {
    downloadShowing = false;/*  w  ww  .  ja va  2 s .  c o  m*/
    if (playShowing) {
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_ID_DOWNLOADING);
    } else {
        downloadForeground = false;
        handler.post(new Runnable() {
            @Override
            public void run() {
                stopForeground(downloadService, true);
            }
        });
    }
}

From source file:mobisocial.bento.ebento.ui.PeopleListFragment.java

public void refreshView() {
    Handler handler = new Handler();
    handler.post(new Runnable() {
        public void run() {
            mListAdapter.notifyDataSetChanged();
            mListView.invalidateViews();
        }//www  .  j  a  v  a 2  s .  c  om
    });
}