function delay() { // `delay` returns a promise return new Promise(function(resolve, reject) { // Only `delay` is able to resolve or reject the promise setTimeout(function() { resolve(42); // After 3 seconds, resolve the promise with value 42 }, 3000); }); } delay().then(function(v) { // `delay` returns a promise console.log(v); // Log the value once it is resolved }).catch(function(v) { // Or do something else if it is rejected // (it would not happen in this example, since `reject` is not called). });
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(browserIntent);
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); }
String url = "http://www.example.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i);
@Override public Object apply(final Object integer) throws Exception { return new Object(); } })
@Override public Flowable<Object> apply(Object opening) { return Flowable.unsafeCreate(new Publisher<Object>() { @Override public void subscribe(Subscriber<? super Object> subscriber) { subscriber.onSubscribe(new BooleanSubscription()); push(subscriber, new Object(), 100); complete(subscriber, 101); } }); } };
@Override public Observable<Object> apply(Object opening) { return Observable.unsafeCreate(new ObservableSource<Object>() { @Override public void subscribe(Observer<? super Object> observer) { observer.onSubscribe(Disposables.empty()); push(observer, new Object(), 100); complete(observer, 101); } }); } };
@Override public Observable<Object> apply(Object opening) { return Observable.unsafeCreate(new ObservableSource<Object>() { @Override public void subscribe(Observer<? super Object> innerObserver) { innerObserver.onSubscribe(Disposables.empty()); push(innerObserver, new Object(), 100); complete(innerObserver, 101); } }); } };
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Single<Object> fromCallableSingle = Single.fromCallable(func); verifyZeroInteractions(func); fromCallableSingle.subscribe(); verify(func).call(); }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Observable<Object> fromCallableObservable = Observable.fromCallable(func); verifyZeroInteractions(func); fromCallableObservable.subscribe(); verify(func).call(); }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func); verifyZeroInteractions(func); fromCallableFlowable.subscribe(); verify(func).call(); }
String url = "url you want to download"; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDescription("Some descrition"); request.setTitle("Some title"); // in order for this if to run, you must use the android 3.2 to compile your app if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); } request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext"); // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request);
@Test public void reduceMaybeCheckDisposed() { TestHelper.checkDisposed(Observable.just(new Object()).reduce(new BiFunction<Object, Object, Object>() { @Override public Object apply(Object a, Object b) throws Exception { return a; } })); }
@Test public void justUnsubscribed() throws Exception { o = new Object(); WeakReference<Object> wr = new WeakReference<Object>(o); TestObserver<Long> to = Observable.just(o).count().toObservable().onTerminateDetach().test(); o = null; to.cancel(); System.gc(); Thread.sleep(200); Assert.assertNull("Object retained!", wr.get()); }
@Test public void flatMapError() { RuntimeException exception = new RuntimeException("test"); Single.error(exception).flatMap(new Function<Object, SingleSource<Object>>() { @Override public SingleSource<Object> apply(final Object integer) throws Exception { return Single.just(new Object()); } }) .test() .assertError(exception); }
@Test public void flatMapSingleError() { RuntimeException exception = new RuntimeException("test"); Maybe.error(exception).flatMapSingle(new Function<Object, SingleSource<Object>>() { @Override public SingleSource<Object> apply(final Object integer) throws Exception { return Single.just(new Object()); } }) .test() .assertError(exception); }
@Test public void flatMapMaybeError() { RuntimeException exception = new RuntimeException("test"); Single.error(exception).flatMapMaybe(new Function<Object, MaybeSource<Object>>() { @Override public MaybeSource<Object> apply(final Object integer) throws Exception { return Maybe.just(new Object()); } }) .test() .assertError(exception); }
@Test public void flatMapSingleElementError() { RuntimeException exception = new RuntimeException("test"); Maybe.error(exception).flatMapSingleElement(new Function<Object, SingleSource<Object>>() { @Override public SingleSource<Object> apply(final Object integer) throws Exception { return Single.just(new Object()); } }) .test() .assertError(exception); }