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:com.bofsoft.laio.customerservice.Fragment.CarListFragment.java

private void set_eSearch_TextChanged() {
    et_carlist_search.addTextChangedListener(new TextWatcher() {

        @Override//from   w w w  .j  a v  a  2  s  . c om
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            //handler.post(eChanged);
            new Handler(Looper.getMainLooper()).post(eChanged);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            /**?? 
             * ????listview???
             * ??
             */
            if (s.length() == 0) {
                //                    ivDeleteText.setVisibility(View.GONE);//??
            } else {
                //                    ivDeleteText.setVisibility(View.VISIBLE);//???

            }
        }
    });

}

From source file:com.nick.scalpel.core.AutoFoundWirer.java

private void wireFromContext(Context context, AutoFound.Type type, int idRes, Resources.Theme theme,
        Field field, Object forWho) {
    Resources resources = context.getResources();
    switch (type) {
    case STRING:/*from www  .  ja v  a 2  s .  c o  m*/
        setField(field, forWho, resources.getString(idRes));
        break;
    case COLOR:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, resources.getColor(idRes, theme));
        } else {
            //noinspection deprecation
            setField(field, forWho, resources.getColor(idRes));
        }
        break;
    case INTEGER:
        setField(field, forWho, resources.getInteger(idRes));
        break;
    case BOOL:
        setField(field, forWho, resources.getBoolean(idRes));
        break;
    case STRING_ARRAY:
        setField(field, forWho, resources.getStringArray(idRes));
        break;
    case INT_ARRAY:
        setField(field, forWho, resources.getIntArray(idRes));
        break;
    case PM:
        setField(field, forWho, context.getSystemService(Context.POWER_SERVICE));
        break;
    case ACCOUNT:
        setField(field, forWho, context.getSystemService(Context.ACCOUNT_SERVICE));
        break;
    case ALARM:
        setField(field, forWho, context.getSystemService(Context.ALARM_SERVICE));
        break;
    case AM:
        setField(field, forWho, context.getSystemService(Context.ACTIVITY_SERVICE));
        break;
    case WM:
        setField(field, forWho, context.getSystemService(Context.WINDOW_SERVICE));
        break;
    case NM:
        setField(field, forWho, context.getSystemService(Context.NOTIFICATION_SERVICE));
        break;
    case TM:
        setField(field, forWho, context.getSystemService(Context.TELEPHONY_SERVICE));
        break;
    case TCM:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.TELECOM_SERVICE));
        }
        break;
    case SP:
        setField(field, forWho, PreferenceManager.getDefaultSharedPreferences(context));
        break;
    case PKM:
        setField(field, forWho, context.getPackageManager());
        break;
    case HANDLE:
        setField(field, forWho, new Handler(Looper.getMainLooper()));
        break;
    case ASM:
        setField(field, forWho, context.getSystemService(Context.ACCESSIBILITY_SERVICE));
        break;
    case CAP:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setField(field, forWho, context.getSystemService(Context.CAPTIONING_SERVICE));
        }
        break;
    case KGD:
        setField(field, forWho, context.getSystemService(Context.KEYGUARD_SERVICE));
        break;
    case LOCATION:
        setField(field, forWho, context.getSystemService(Context.LOCATION_SERVICE));
        break;
    case SEARCH:
        setField(field, forWho, context.getSystemService(Context.SEARCH_SERVICE));
        break;
    case SENSOR:
        setField(field, forWho, context.getSystemService(Context.SENSOR_SERVICE));
        break;
    case STORAGE:
        setField(field, forWho, context.getSystemService(Context.STORAGE_SERVICE));
        break;
    case WALLPAPER:
        setField(field, forWho, context.getSystemService(Context.WALLPAPER_SERVICE));
        break;
    case VIBRATOR:
        setField(field, forWho, context.getSystemService(Context.VIBRATOR_SERVICE));
        break;
    case CONNECT:
        setField(field, forWho, context.getSystemService(Context.CONNECTIVITY_SERVICE));
        break;
    case NETWORK_STATUS:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, context.getSystemService(Context.NETWORK_STATS_SERVICE));
        }
        break;
    case WIFI:
        setField(field, forWho, context.getSystemService(Context.WIFI_SERVICE));
        break;
    case AUDIO:
        setField(field, forWho, context.getSystemService(Context.AUDIO_SERVICE));
        break;
    case FP:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            setField(field, forWho, context.getSystemService(Context.FINGERPRINT_SERVICE));
        }
        break;
    case MEDIA_ROUTER:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setField(field, forWho, context.getSystemService(Context.MEDIA_ROUTER_SERVICE));
        }
        break;
    case SUB:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            setField(field, forWho, context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE));
        }
        break;
    case IME:
        setField(field, forWho, context.getSystemService(Context.INPUT_METHOD_SERVICE));
        break;
    case CLIP_BOARD:
        setField(field, forWho, context.getSystemService(Context.CLIPBOARD_SERVICE));
        break;
    case APP_WIDGET:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.APPWIDGET_SERVICE));
        }
        break;
    case DEVICE_POLICY:
        setField(field, forWho, context.getSystemService(Context.DEVICE_POLICY_SERVICE));
        break;
    case DOWNLOAD:
        setField(field, forWho, context.getSystemService(Context.DOWNLOAD_SERVICE));
        break;
    case BATTERY:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setField(field, forWho, context.getSystemService(Context.BATTERY_SERVICE));
        }
        break;
    case NFC:
        setField(field, forWho, context.getSystemService(Context.NFC_SERVICE));
        break;
    case DISPLAY:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setField(field, forWho, context.getSystemService(Context.DISPLAY_SERVICE));
        }
        break;
    case USER:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            setField(field, forWho, context.getSystemService(Context.USER_SERVICE));
        }
        break;
    case APP_OPS:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setField(field, forWho, context.getSystemService(Context.APP_OPS_SERVICE));
        }
        break;
    case BITMAP:
        setField(field, forWho, BitmapFactory.decodeResource(resources, idRes, null));
        break;
    }
}

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  w  w.j  av a  2 s  .  co m*/
        }
    });
}

From source file:com.android.plugins.MagnetometerListener.java

/**
 * Start listening for speed sensor.//from w  w  w  .  jav a2 s  .  c o  m
 *
 * @return          status of listener
*/
private int start() {
    // If already starting or running, then just return
    if ((this.status == MagnetometerListener.RUNNING) || (this.status == MagnetometerListener.STARTING)) {
        return this.status;
    }

    this.setStatus(MagnetometerListener.STARTING);

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

    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mSensor = list.get(0);
        this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI);
        this.setStatus(MagnetometerListener.STARTING);
    } else {
        this.setStatus(MagnetometerListener.ERROR_FAILED_TO_START);
        this.fail(MagnetometerListener.ERROR_FAILED_TO_START,
                "No sensors found to register magnetometer 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: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);
            }/*ww w  . ja  va  2 s .  c om*/
        }
    });
}

From source file:Main.java

public static Handler runInUIThread(final Runnable runnable, boolean runImmediatelyIfPossible) {
    if (runnable == null) {
        return null;
    }//from  w ww.j  av a 2  s. co m

    final Handler handler;
    Looper mainLooper = Looper.getMainLooper();
    if (runImmediatelyIfPossible && (Thread.currentThread() == mainLooper.getThread())) {
        handler = null;
        runnable.run();
    } else {
        handler = new Handler(mainLooper);
        handler.post(runnable);
    }

    return handler;
}

From source file:com.android.plugins.GyroscopeListener.java

/**
 * Start listening for speed sensor./*from   w  ww  . j a  v a2s.  c o m*/
 *
 * @return          status of listener
*/
private int start() {
    // If already starting or running, then just return
    if ((this.status == GyroscopeListener.RUNNING) || (this.status == GyroscopeListener.STARTING)) {
        return this.status;
    }

    this.setStatus(GyroscopeListener.STARTING);

    // Get gyroscope from sensor manager
    List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_GYROSCOPE);

    // If found, then register as listener
    if ((list != null) && (list.size() > 0)) {
        this.mSensor = list.get(0);
        this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI);
        this.setStatus(GyroscopeListener.STARTING);
    } else {
        this.setStatus(GyroscopeListener.ERROR_FAILED_TO_START);
        this.fail(GyroscopeListener.ERROR_FAILED_TO_START,
                "No sensors found to register gyroscope 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.CaptchaActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    PrefsUtility.applyTheme(this);
    getSupportActionBar().setTitle(R.string.post_captcha_title);

    super.onCreate(savedInstanceState);

    final LoadingView loadingView = new LoadingView(this, R.string.download_waiting, true, true);
    setContentView(loadingView);// ww  w.ja  va 2  s  .  c o  m

    final RedditAccount selectedAccount = RedditAccountManager.getInstance(this)
            .getAccount(getIntent().getStringExtra("username"));

    final CacheManager cm = CacheManager.getInstance(this);

    RedditAPI.newCaptcha(cm, new APIResponseHandler.NewCaptchaResponseHandler(this) {
        @Override
        protected void onSuccess(final String captchaId) {

            final URI captchaUrl = Constants.Reddit.getUri("/captcha/" + captchaId);

            cm.makeRequest(new CacheRequest(captchaUrl, RedditAccountManager.getAnon(), null,
                    Constants.Priority.CAPTCHA, 0, CacheRequest.DownloadType.FORCE, Constants.FileType.CAPTCHA,
                    false, false, true, CaptchaActivity.this) {
                @Override
                protected void onCallbackException(Throwable t) {
                    BugReportActivity.handleGlobalError(CaptchaActivity.this, t);
                }

                @Override
                protected void onDownloadNecessary() {
                }

                @Override
                protected void onDownloadStarted() {
                    loadingView.setIndeterminate(R.string.download_downloading);
                }

                @Override
                protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                        String readableMessage) {
                    final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type, t,
                            status, url.toString());
                    General.showResultDialog(CaptchaActivity.this, error);
                    finish();
                }

                @Override
                protected void onProgress(long bytesRead, long totalBytes) {
                    loadingView.setProgress(R.string.download_downloading,
                            (float) ((double) bytesRead / (double) totalBytes));
                }

                @Override
                protected void onSuccess(final CacheManager.ReadableCacheFile cacheFile, long timestamp,
                        UUID session, boolean fromCache, String mimetype) {

                    final Bitmap image;
                    try {
                        image = BitmapFactory.decodeStream(cacheFile.getInputStream());
                    } catch (IOException e) {
                        BugReportActivity.handleGlobalError(CaptchaActivity.this, e);
                        return;
                    }

                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        public void run() {

                            final LinearLayout ll = new LinearLayout(CaptchaActivity.this);
                            ll.setOrientation(LinearLayout.VERTICAL);

                            final ImageView captchaImg = new ImageView(CaptchaActivity.this);
                            ll.addView(captchaImg);
                            final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) captchaImg
                                    .getLayoutParams();
                            layoutParams.setMargins(20, 20, 20, 20);
                            layoutParams.height = General.dpToPixels(context, 100);
                            captchaImg.setScaleType(ImageView.ScaleType.FIT_CENTER);

                            final EditText captchaText = new EditText(CaptchaActivity.this);
                            ll.addView(captchaText);
                            ((LinearLayout.LayoutParams) captchaText.getLayoutParams()).setMargins(20, 0, 20,
                                    20);
                            captchaText.setInputType(android.text.InputType.TYPE_CLASS_TEXT
                                    | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                                    | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

                            captchaImg.setImageBitmap(image);

                            final Button submitButton = new Button(CaptchaActivity.this);
                            submitButton.setText(R.string.post_captcha_submit_button);
                            ll.addView(submitButton);
                            ((LinearLayout.LayoutParams) submitButton.getLayoutParams()).setMargins(20, 0, 20,
                                    20);
                            ((LinearLayout.LayoutParams) submitButton
                                    .getLayoutParams()).gravity = Gravity.RIGHT;
                            ((LinearLayout.LayoutParams) submitButton
                                    .getLayoutParams()).width = LinearLayout.LayoutParams.WRAP_CONTENT;

                            submitButton.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    final Intent result = new Intent();
                                    result.putExtra("captchaId", captchaId);
                                    result.putExtra("captchaText", captchaText.getText().toString());
                                    setResult(RESULT_OK, result);
                                    finish();
                                }
                            });

                            final ScrollView sv = new ScrollView(CaptchaActivity.this);
                            sv.addView(ll);
                            setContentView(sv);
                        }
                    });

                }
            });
        }

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

        @Override
        protected void onFailure(RequestFailureType type, Throwable t, StatusLine status,
                String readableMessage) {
            final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type, t, status,
                    null);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
        }

        @Override
        protected void onFailure(APIFailureType type) {
            final RRError error = General.getGeneralErrorForFailure(CaptchaActivity.this, type);
            General.showResultDialog(CaptchaActivity.this, error);
            finish();
        }
    }, selectedAccount, this);
}

From source file:com.module.candychat.net.activity.PictureActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_share) {

        Handler uiHandler = new Handler(Looper.getMainLooper());
        uiHandler.post(new Runnable() {
            @Override/*  w  w w .ja  va  2 s.  c  o  m*/
            public void run() {

                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, getShareImageUri(mImageTitle, mImageUrl));
                shareIntent.setType("image/jpg");
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(Intent.createChooser(shareIntent, "Share..."));

                //ShareUtils.shareImage(getApplicationContext(), getShareImageUri(mImageTitle,mImageUrl), "Share...");
            }
        });

        //return true;
    } else if (id == R.id.action_save) {

        Handler uiHandler = new Handler(Looper.getMainLooper());
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                saveImageToGallery();
            }
        });

        //return true;
    } else if (id == android.R.id.home) {
        finish();
    }

    return super.onOptionsItemSelected(item);
}

From source file:com.appnexus.opensdk.ANNativeAdResponse.java

/**
 * Process the metadata of native response from ad server
 *
 * @param metaData JsonObject that contains info of native ad
 * @return ANNativeResponse if no issue happened during processing
 *///from w w w  . j a va  2s .  com
static ANNativeAdResponse create(JSONObject metaData) {
    if (metaData == null) {
        return null;
    }
    JSONArray impTrackerJson = JsonUtil.getJSONArray(metaData, KEY_IMP_TRACK);
    ArrayList<String> imp_trackers = JsonUtil.getStringArrayList(impTrackerJson);
    if (imp_trackers == null) {
        return null;
    }
    ANNativeAdResponse response = new ANNativeAdResponse();
    response.imp_trackers = imp_trackers;
    response.title = JsonUtil.getJSONString(metaData, KEY_TITLE);
    response.description = JsonUtil.getJSONString(metaData, KEY_DESCRIPTION);
    JSONArray main_media = JsonUtil.getJSONArray(metaData, KEY_MAIN_MEDIA);
    if (main_media != null) {
        int l = main_media.length();
        for (int i = 0; i < l; i++) {
            JSONObject media = JsonUtil.getJSONObjectFromArray(main_media, i);
            if (media != null) {
                String label = JsonUtil.getJSONString(media, KEY_IMAGE_LABEL);
                if (label != null && label.equals(VALUE_DEFAULT_IMAGE)) {
                    response.imageUrl = JsonUtil.getJSONString(media, KEY_IMAGE_URL);
                    break;
                }
            }
        }
    }
    ;
    response.iconUrl = JsonUtil.getJSONString(metaData, KEY_ICON);
    response.socialContext = JsonUtil.getJSONString(metaData, KEY_CONTEXT);
    response.callToAction = JsonUtil.getJSONString(metaData, KEY_CTA);
    response.clickUrl = JsonUtil.getJSONString(metaData, KEY_CLICK_URL);
    response.clickFallBackUrl = JsonUtil.getJSONString(metaData, KEY_CLICK_FALLBACK_URL);
    JSONObject rating = JsonUtil.getJSONObject(metaData, KEY_RATING);
    response.rating = new Rating(JsonUtil.getJSONDouble(rating, KEY_RATING_VALUE),
            JsonUtil.getJSONDouble(rating, KEY_RATING_SCALE));
    JSONArray clickTrackerJson = JsonUtil.getJSONArray(metaData, KEY_CLICK_TRACK);
    response.click_trackers = JsonUtil.getStringArrayList(clickTrackerJson);
    JSONObject custom = JsonUtil.getJSONObject(metaData, KEY_CUSTOM);
    response.nativeElements = JsonUtil.getStringObjectHashMap(custom);
    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(response.expireRunnable, Settings.NATIVE_AD_RESPONSE_EXPIRATION_TIME);
    return response;
}