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:org.quantumbadger.redreader.common.General.java

public static void quickToast(final Context context, final String text, final int duration) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        public void run() {
            Toast.makeText(context, text, duration).show();
        }// w  w  w  .  j a v  a2  s  . c  o  m
    });
}

From source file:com.squareup.picasso3.Utils.java

static boolean isMain() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

From source file:com.ryan.ryanreader.common.General.java

public static void quickToast(final Context context, final String text) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
        public void run() {
            Toast.makeText(context, text, Toast.LENGTH_LONG).show();
        }// w  w w .j a v a 2 s .  com
    });
}

From source file:ota.otaupdates.utils.Utils.java

public static Boolean isMainThread() {
    return Looper.myLooper() == Looper.getMainLooper();
}

From source file:com.cloudant.todo.TasksModel.java

public TasksModel(Context context) {

    this.mContext = context;

    // Set up our tasks datastore within its own folder in the applications
    // data directory.
    File path = this.mContext.getApplicationContext().getDir(DATASTORE_MANGER_DIR, Context.MODE_PRIVATE);
    DatastoreManager manager = new DatastoreManager(path.getAbsolutePath());
    try {/* w w  w .j  av a2s.c o  m*/
        this.mDatastore = manager.openDatastore(TASKS_DATASTORE_NAME);
    } catch (DatastoreNotCreatedException dnce) {
        Log.e(LOG_TAG, "Unable to open Datastore", dnce);
    }

    Log.d(LOG_TAG, "Set up database at " + path.getAbsolutePath());

    // Set up the replicator objects from the app's settings.
    try {
        this.reloadReplicationSettings();
    } catch (URISyntaxException e) {
        Log.e(LOG_TAG, "Unable to construct remote URI from configuration", e);
    }

    // Allow us to switch code called by the ReplicationListener into
    // the main thread so the UI can update safely.
    this.mHandler = new Handler(Looper.getMainLooper());

    Log.d(LOG_TAG, "TasksModel set up " + path.getAbsolutePath());
}

From source file:com.facebook.android.Places.java

public void getLocation() {
    /*/*w w  w.java  2s . c  o  m*/
     * launch a new Thread to get new location
     */
    new Thread() {
        @Override
        public void run() {
            Looper.prepare();
            dialog = ProgressDialog.show(Places.this, "", getString(R.string.fetching_location), false, true,
                    new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            showToast("No location fetched.");
                        }
                    });

            if (lm == null) {
                lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            }

            if (locationListener == null) {
                locationListener = new MyLocationListener();
            }

            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            if (provider != null && lm.isProviderEnabled(provider)) {
                lm.requestLocationUpdates(provider, 1, 0, locationListener, Looper.getMainLooper());
            } else {
                /*
                 * GPS not enabled, prompt user to enable GPS in the
                 * Location menu
                 */
                new AlertDialog.Builder(Places.this).setTitle(R.string.enable_gps_title)
                        .setMessage(getString(R.string.enable_gps))
                        .setPositiveButton(R.string.gps_settings, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                startActivityForResult(
                                        new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                                        0);
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Places.this.finish();
                            }
                        }).show();
            }
            Looper.loop();
        }
    }.start();
}

From source file:com.tencent.tinker.app.TinkerServerManager.java

/**
 * ???// w ww  .ja  va  2 s  . c  o  m
 * @param immediately ?,?
 */
public static void checkTinkerUpdate(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.checkTinkerUpdate(immediately);
                return false;
            }
        });
    }
}

From source file:com.rxsampleapp.RxApiTestActivity.java

public void getAllUsers(View view) {
    RxAndroidNetworking.get(ApiEndPoint.BASE_URL + ApiEndPoint.GET_JSON_ARRAY)
            .addPathParameter("pageNumber", "0").addQueryParameter("limit", "3").build()
            .setAnalyticsListener(new AnalyticsListener() {
                @Override/*www  .j  a  va 2 s  .c  om*/
                public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived,
                        boolean isFromCache) {
                    Log.d(TAG, " timeTakenInMillis : " + timeTakenInMillis);
                    Log.d(TAG, " bytesSent : " + bytesSent);
                    Log.d(TAG, " bytesReceived : " + bytesReceived);
                    Log.d(TAG, " isFromCache : " + isFromCache);
                }
            }).getParseObservable(new TypeToken<List<User>>() {
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<List<User>>() {
                @Override
                public void onCompleted() {
                    Log.d(TAG, "onComplete Detail : getAllUsers completed");
                }

                @Override
                public void onError(Throwable e) {
                    Utils.logError(TAG, e);
                }

                @Override
                public void onNext(List<User> users) {
                    Log.d(TAG, "onResponse isMainThread : "
                            + String.valueOf(Looper.myLooper() == Looper.getMainLooper()));
                    Log.d(TAG, "userList size : " + users.size());
                    for (User user : users) {
                        Log.d(TAG, "id : " + user.id);
                        Log.d(TAG, "firstname : " + user.firstname);
                        Log.d(TAG, "lastname : " + user.lastname);
                    }
                }
            });
}

From source file:com.frostwire.android.gui.adapters.menu.FileListAdapter.java

@Override
protected MenuAdapter getMenuAdapter(View view) {
    Handler h = new Handler(Looper.getMainLooper());
    h.sendEmptyMessage(5011105);//from   w  w w .  j a v a  2s.  c  o  m

    Context context = getContext();

    List<MenuAction> items = new ArrayList<>();

    // due to long click generic handle
    FileDescriptor fd = null;

    if (view.getTag() instanceof FileDescriptorItem) {
        FileDescriptorItem item = (FileDescriptorItem) view.getTag();
        fd = item.fd;
    } else if (view.getTag() instanceof FileDescriptor) {
        fd = (FileDescriptor) view.getTag();
    }

    if (checkIfNotExists(fd)) {
        return null;
    }

    List<FileDescriptor> checked = convertItems(getChecked());
    ensureCorrectMimeType(fd);
    boolean canOpenFile = fd.mime != null
            && (fd.mime.contains("audio") || fd.mime.contains("bittorrent") || fd.filePath != null);
    int numChecked = checked.size();

    boolean showSingleOptions = showSingleOptions(checked, fd);

    if (TransferManager.canSeedFromMyFilesTempHACK()) {
        if (showSingleOptions) {
            items.add(new SeedAction(context, fd));
        } else {
            items.add(new SeedAction(context, checked));
        }
    }

    if (showSingleOptions) {
        if (canOpenFile) {
            items.add(new OpenMenuAction(context, fd.filePath, fd.mime));
        }

        if ((fd.fileType == Constants.FILE_TYPE_RINGTONES || fd.fileType == Constants.FILE_TYPE_AUDIO)
                && numChecked <= 1) {
            items.add(new SetAsRingtoneMenuAction(context, fd));
        }

        if (fd.fileType == Constants.FILE_TYPE_PICTURES && numChecked <= 1) {
            items.add(new SetAsWallpaperMenuAction(context, fd));
        }

        if (fd.fileType != Constants.FILE_TYPE_APPLICATIONS && numChecked <= 1) {
            items.add(new RenameFileMenuAction(context, this, fd));
        }

        if (fd.mime != null && fd.mime.equals(Constants.MIME_TYPE_BITTORRENT) && numChecked <= 1) {
            items.add(new CopyToClipboardMenuAction(context, R.drawable.contextmenu_icon_magnet,
                    R.string.transfers_context_menu_copy_magnet,
                    R.string.transfers_context_menu_copy_magnet_copied,
                    new MagnetUriBuilder(fd.filePath).getMagnet()));

            items.add(new CopyToClipboardMenuAction(context, R.drawable.contextmenu_icon_copy,
                    R.string.transfers_context_menu_copy_infohash,
                    R.string.transfers_context_menu_copy_infohash_copied, new InfoHashBuilder(fd.filePath)));
        }
    }

    List<FileDescriptor> list = checked;
    if (list.size() == 0) {
        list = Arrays.asList(fd);
    }

    if (fd.fileType == Constants.FILE_TYPE_AUDIO) {
        items.add(new AddToPlaylistMenuAction(context, list));
    }

    if (fd.fileType != Constants.FILE_TYPE_APPLICATIONS) {
        items.add(new SendFileMenuAction(context, fd));
        items.add(new DeleteFileMenuAction(context, this, list));
    }

    return new MenuAdapter(context, fd.title, items);
}

From source file:com.f2prateek.dfg.core.GenerateFrameService.java

@Override
public void doneImage(final Uri imageUri) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override/*from   ww w  .  j  a va 2s  .c o  m*/
        public void run() {
            bus.post(new Events.SingleImageProcessed(device, imageUri));
        }
    });

    // Create the intent to let the user share the image
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    Intent chooserIntent = Intent.createChooser(sharingIntent, null);
    chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationBuilder.addAction(R.drawable.ic_action_share, getResources().getString(R.string.share),
            PendingIntent.getActivity(this, 0, chooserIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    // Create the intent to let the user rate the app
    Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(AppConstants.MARKET_URL));
    notificationBuilder.addAction(R.drawable.ic_action_rate, getResources().getString(R.string.rate),
            PendingIntent.getActivity(this, 0, rateIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    // Create the intent to show the screenshot in gallery
    Intent launchIntent = new Intent(Intent.ACTION_VIEW);
    launchIntent.setDataAndType(imageUri, "image/png");
    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationBuilder.setContentTitle(resources.getString(R.string.screenshot_saved_title))
            .setContentText(resources.getString(R.string.single_screenshot_saved, device.name()))
            .setContentIntent(PendingIntent.getActivity(this, 0, launchIntent, 0))
            .setWhen(System.currentTimeMillis()).setProgress(0, 0, false).setAutoCancel(true);

    notificationManager.notify(DFG_NOTIFICATION_ID, notificationBuilder.build());
}