List of usage examples for android.os Looper getMainLooper
public static Looper getMainLooper()
From source file:com.android.volley.RequestQueue.java
/** * Creates the worker pool. Processing will not begin until {@link #start()} is called. * * @param cache A Cache to use for persisting responses to disk * @param network A Network interface for performing HTTP requests * @param threadPoolSize Number of network dispatcher threads to create *//*from ww w. j a v a 2 s. c om*/ public RequestQueue(Cache cache, Network network, int threadPoolSize) { this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper()))); }
From source file:com.ryan.ryanreader.fragments.AddAccountDialog.java
@Override public Dialog onCreateDialog(final Bundle savedInstanceState) { if (alreadyCreated) return getDialog(); alreadyCreated = true;/*from w w w. j a v a 2 s. c o m*/ super.onCreateDialog(savedInstanceState); final AlertDialog.Builder builder = new AlertDialog.Builder(getSupportActivity()); builder.setTitle(R.string.accounts_add); final View view = getSupportActivity().getLayoutInflater().inflate(R.layout.dialog_login, null); builder.setView(view); builder.setCancelable(true); final EditText usernameBox = ((EditText) view.findViewById(R.id.login_username)); usernameBox.setText(lastUsername); usernameBox.requestFocus(); usernameBox.requestFocusFromTouch(); builder.setPositiveButton(R.string.accounts_login, new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialogInterface, final int i) { final String username = ((EditText) getDialog().findViewById(R.id.login_username)).getText() .toString().trim(); final String password = ((EditText) getDialog().findViewById(R.id.login_password)).getText() .toString(); lastUsername = username; final ProgressDialog progressDialog = new ProgressDialog(getSupportActivity()); final Thread thread; progressDialog.setTitle(R.string.accounts_loggingin); progressDialog.setMessage(getString(R.string.accounts_loggingin_msg)); progressDialog.setIndeterminate(true); final AtomicBoolean cancelled = new AtomicBoolean(false); progressDialog.setCancelable(true); progressDialog.setCanceledOnTouchOutside(false); progressDialog.show(); progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(final DialogInterface dialogInterface) { cancelled.set(true); progressDialog.dismiss(); } }); progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { public boolean onKey(final DialogInterface dialogInterface, final int keyCode, final KeyEvent keyEvent) { if (keyCode == KeyEvent.KEYCODE_BACK) { cancelled.set(true); progressDialog.dismiss(); } return true; } }); thread = new Thread() { @Override public void run() { // TODO better HTTP client final RedditAccount.LoginResultPair result = RedditAccount.login(getSupportActivity(), username, password, new DefaultHttpClient()); new Handler(Looper.getMainLooper()).post(new Runnable() { public void run() { if (cancelled.get()) return; // safe, since we're in the UI thread progressDialog.dismiss(); final AlertDialog.Builder alertBuilder = new AlertDialog.Builder( getSupportActivity()); alertBuilder.setNeutralButton(R.string.dialog_close, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { new AccountListDialog().show(getSupportActivity()); } }); // TODO handle errors better switch (result.result) { case CONNECTION_ERROR: alertBuilder.setTitle(R.string.error_connection_title); alertBuilder.setMessage(R.string.message_cannotlogin); break; case INTERNAL_ERROR: alertBuilder.setTitle(R.string.error_unknown_title); alertBuilder.setMessage(R.string.message_cannotlogin); break; case JSON_ERROR: alertBuilder.setTitle(R.string.error_parse_title); alertBuilder.setMessage(R.string.message_cannotlogin); break; case REQUEST_ERROR: alertBuilder.setTitle(R.string.error_connection_title); alertBuilder.setMessage(R.string.message_cannotlogin); break; case UNKNOWN_REDDIT_ERROR: alertBuilder.setTitle(R.string.error_unknown_title); alertBuilder.setMessage(R.string.message_cannotlogin); break; case WRONG_PASSWORD: alertBuilder.setTitle(R.string.error_invalid_password_title); alertBuilder.setMessage(R.string.error_invalid_password_message); break; case RATELIMIT: alertBuilder.setTitle(R.string.error_ratelimit_title); alertBuilder.setMessage(String.format("%s \"%s\"", getString(R.string.error_ratelimit_message), result.extraMessage)); break; case SUCCESS: RedditAccountManager.getInstance(getSupportActivity()) .addAccount(result.account); RedditAccountManager.getInstance(getSupportActivity()) .setDefaultAccount(result.account); alertBuilder.setTitle(R.string.general_success); alertBuilder.setMessage(R.string.message_nowloggedin); lastUsername = ""; break; default: throw new RuntimeException(); } final AlertDialog alertDialog = alertBuilder.create(); alertDialog.show(); } }); } }; thread.start(); } }); builder.setNegativeButton(R.string.dialog_cancel, null); return builder.create(); }
From source file:com.tencent.tinker.app.TinkerServerManager.java
/** * ????/*from w w w. j a v a2s. c om*/ * @param configRequestCallback * @param immediately ?,? */ public static void getDynamicConfig(final ConfigRequestCallback configRequestCallback, final boolean immediately) { if (sTinkerServerClient == null) { TinkerLog.e(TAG, "checkTinkerUpdate, sTinkerServerClient == null"); return; } Tinker tinker = sTinkerServerClient.getTinker(); //only check at the main process if (tinker.isMainProcess()) { Looper.getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() { @Override public boolean queueIdle() { sTinkerServerClient.getDynamicConfig(configRequestCallback, immediately); return false; } }); } }
From source file:io.selendroid.ServerInstrumentation.java
@SuppressWarnings("unchecked") @Override/*from www . j av a 2 s .c om*/ public void onCreate(Bundle arguments) { String activityClazzName = arguments.getString("main_activity"); int parsedServerPort = 0; try { String port = arguments.getString("server_port"); if (port != null && port.isEmpty() == false) { parsedServerPort = Integer.parseInt(port); } } catch (NumberFormatException ex) { SelendroidLogger.log("Unable to parse the value of server_port key."); parsedServerPort = this.serverPort; } if (isValidPort(parsedServerPort)) { this.serverPort = parsedServerPort; } Class<? extends Activity> clazz = null; try { clazz = (Class<? extends Activity>) Class.forName(activityClazzName); } catch (ClassNotFoundException exception) { SelendroidLogger.log("The class with name '" + activityClazzName + "' does not exist.", exception); } mainActivity = clazz; SelendroidLogger.log("Instrumentation initialized with main activity: " + activityClazzName); if (clazz == null) { SelendroidLogger.log("Clazz is null - but should be an instance of: " + activityClazzName); } instance = this; mainThreadExecutor = provideMainThreadExecutor(Looper.getMainLooper()); uiController = new UiThreadController(); start(); }
From source file:com.samknows.measurement.schedule.datacollection.LocationDataCollector.java
@Override public void start(TestContext tc) { super.start(tc); locations = Collections.synchronizedList(new ArrayList<Location>()); manager = (LocationManager) tc.getSystemService(Context.LOCATION_SERVICE); locationType = AppSettings.getInstance().getLocationServiceType(); //if the provider in the settings is gps but the service is not enable fail over to network provider if (locationType == LocationType.gps && !manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { locationType = LocationType.network; }/*from www . ja v a2 s . c o m*/ if (locationType != LocationType.gps && locationType != LocationType.network) { throw new RuntimeException("unknown location type: " + locationType); } String provider = locationType == LocationType.gps ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER; if (getLastKnown) { lastKnown = manager.getLastKnownLocation(provider); if (lastKnown != null) { data.add(new LocationData(true, lastKnown, locationType)); lastKnownLocation = locationToDCSString("LASTKNOWNLOCATION", lastKnown); } } gotLastLocation = false; manager.requestLocationUpdates(provider, 0, 0, LocationDataCollector.this, Looper.getMainLooper()); Logger.d(this, "start collecting location data from: " + provider); try { Logger.d(this, "sleeping: " + time); Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } //stop listening for location updates if we are on network. That is done because network location uses network and breaks NetworkCondition if (locationType == LocationType.network) { manager.removeUpdates(this); } }
From source file:com.groundupworks.wings.dropbox.DropboxEndpoint.java
/** * Links an account in a background thread. If unsuccessful, the link error is handled on a ui thread and a * {@link Toast} will be displayed.//from w ww.j a v a 2 s . c om */ private void link() { synchronized (mDropboxApiLock) { if (mDropboxApi != null) { final DropboxAPI<AndroidAuthSession> dropboxApi = mDropboxApi; mHandler.post(new Runnable() { @Override public void run() { String accountName = null; String shareUrl = null; String accessToken = null; // Request params. synchronized (mDropboxApiLock) { // Create directory for storing photos. if (createPhotoFolder(dropboxApi)) { // Get account params. accountName = requestAccountName(dropboxApi); shareUrl = requestShareUrl(dropboxApi); accessToken = dropboxApi.getSession().getOAuth2AccessToken(); } } // Validate account settings and store. Handler uiHandler = new Handler(Looper.getMainLooper()); if (accountName != null && accountName.length() > 0 && shareUrl != null && shareUrl.length() > 0 && accessToken != null) { storeAccountParams(accountName, shareUrl, accessToken); // Emit link state change event on ui thread. uiHandler.post(new Runnable() { @Override public void run() { notifyLinkStateChanged(new LinkEvent(true)); } }); } else { // Handle error on ui thread. uiHandler.post(new Runnable() { @Override public void run() { handleLinkError(); } }); } } }); } } }
From source file:com.facebook.share.internal.VideoUploader.java
private static synchronized Handler getHandler() { if (handler == null) { handler = new Handler(Looper.getMainLooper()); }//from w w w . j a v a 2 s . co m return handler; }
From source file:com.google.zxing.client.android.CaptureActivity.java
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Window window = getWindow();/*from ww w .j a v a 2 s. co m*/ window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(EUExUtil.getResLayoutID("plugin_uexscanner_capture_layout")); if (getIntent() != null) { DataJsonVO data = (DataJsonVO) getIntent().getSerializableExtra(JsConst.DATA_JSON); if (data != null) { mData = data; } else { mData = new DataJsonVO(); } } hasSurface = false; inactivityTimer = new InactivityTimer(this); ambientLightManager = new AmbientLightManager(this); mConRel = (RelativeLayout) findViewById(EUExUtil.getResIdID("plugin_uexscanner_content_rel")); initConView(); mHandler = new ScanPicHandler(Looper.getMainLooper()); }
From source file:com.libreteam.taxi.Customer_Boarding.java
public void didGetPosition(final LatLng driver) { driverLatLng = driver;/* w ww . j a v a 2s . c om*/ Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { } }); }
From source file:com.aniruddhfichadia.presentable.PresentableFragment.java
/** A {@link Fragment} equivalent of {@link android.app.Activity#runOnUiThread(Runnable)}. */ protected void runOnUiThread(@NonNull Runnable runnable) { if (Looper.myLooper() == Looper.getMainLooper()) { // This is the main looper/thread, just execute the runnable runnable.run();//from ww w .j av a 2 s . c o m } else { synchronized (uiHandlerLock) { // Not the main looper, post event on it if (uiHandler == null) { uiHandler = new Handler(Looper.getMainLooper()); } uiHandler.post(runnable); } } }