当前位置: 首页>>代码示例>>Java>>正文


Java Observable.create方法代码示例

本文整理汇总了Java中io.reactivex.Observable.create方法的典型用法代码示例。如果您正苦于以下问题:Java Observable.create方法的具体用法?Java Observable.create怎么用?Java Observable.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.reactivex.Observable的用法示例。


在下文中一共展示了Observable.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: storeFile

import io.reactivex.Observable; //导入方法依赖的package包/类
static Observable<Boolean> storeFile(final FileOutputStream fos, final Bitmap bitmap, final int quality) {
    return Observable.create(new ObservableOnSubscribe<Boolean>() {
        @Override
        public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
            bitmap.recycle();
            try {
                fos.flush();
                fos.close();
                emitter.onNext(true);
            } catch (IOException e) {
                e.printStackTrace();
                emitter.onError(e);
            }
        }
    });
}
 
开发者ID:vshkl,项目名称:PXLSRT,代码行数:18,代码来源:TempStorageUtils.java

示例2: main

import io.reactivex.Observable; //导入方法依赖的package包/类
public static void main(String[] args) {
	String[] monthArray = { "Jan", "Feb", "Mar", "Apl", "May"};
	List<String> months = Arrays.asList(monthArray);

	Observable<Object> observable = Observable.create(subscriber -> {
		try {
			System.out.println("creating ");
			for (String data : months) {
				subscriber.onNext(data);
			}
			subscriber.onComplete();
		} catch (Exception e) {
			// TODO: handle exception
			subscriber.onError(e);
		}
	});
	observable.subscribe(item -> System.out.println("month:-" + item));
	observable.subscribe(item -> System.out.println("month:-" + item));

}
 
开发者ID:PacktPublishing,项目名称:Reactive-Programming-With-Java-9,代码行数:21,代码来源:Demo_MultipleSubscribers.java

示例3: getAllPlaylists

import io.reactivex.Observable; //导入方法依赖的package包/类
@NonNull
public static Observable<ArrayList<Playlist>> getAllPlaylists(@Nullable final Cursor cursor) {
    return Observable.create(e -> {
        ArrayList<Playlist> playlists = new ArrayList<>();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                playlists.add(getPlaylistFromCursorImpl(cursor));
            } while (cursor.moveToNext());
        }
        if (cursor != null)
            cursor.close();

        e.onNext(playlists);
        e.onComplete();
    });
}
 
开发者ID:h4h13,项目名称:RetroMusicPlayer,代码行数:18,代码来源:PlaylistLoader.java

示例4: observable

import io.reactivex.Observable; //导入方法依赖的package包/类
/**
 * The returned Observable emits Query results as Lists.
 * Never completes, so you will get updates when underlying data changes.
 */
public static <T> Observable<List<T>> observable(final Query<T> query) {
    return Observable.create(new ObservableOnSubscribe<List<T>>() {
        @Override
        public void subscribe(final ObservableEmitter<List<T>> emitter) throws Exception {
            final DataSubscription dataSubscription = query.subscribe().observer(new DataObserver<List<T>>() {
                @Override
                public void onData(List<T> data) {
                    if (!emitter.isDisposed()) {
                        emitter.onNext(data);
                    }
                }
            });
            emitter.setCancellable(new Cancellable() {
                @Override
                public void cancel() throws Exception {
                    dataSubscription.cancel();
                }
            });
        }
    });
}
 
开发者ID:greenrobot,项目名称:ObjectBoxRxJava,代码行数:26,代码来源:RxQuery.java

示例5: main

import io.reactivex.Observable; //导入方法依赖的package包/类
public static void main(String[] args) {
    Observable<String> source = Observable.create(emitter -> {
        try {
            emitter.onNext("Alpha");
            emitter.onNext("Beta");
            emitter.onNext("Gamma");
            emitter.onNext("Delta");
            emitter.onNext("Epsilon");
            emitter.onComplete();
        } catch (Throwable e) {
            emitter.onError(e);
        }
    });
    Observable<Integer> lengths = source.map(String::length);
    Observable<Integer> filtered = lengths.filter(i -> i >= 5);
    filtered.subscribe(s -> System.out.println("RECEIVED: " +
            s));
}
 
开发者ID:PacktPublishing,项目名称:Learning-RxJava,代码行数:19,代码来源:Ch2_3.java

示例6: fromBroadcast

import io.reactivex.Observable; //导入方法依赖的package包/类
public static Observable<Intent> fromBroadcast(final Context context, final IntentFilter filter) {
    return Observable.create(e -> {


        final BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context1, Intent intent) {
                e.onNext(intent);
            }

        };
        context.registerReceiver(receiver, filter);
        e.setDisposable(new Disposable() {
            @Override
            public void dispose() {
                context.unregisterReceiver(receiver);
            }

            @Override
            public boolean isDisposed() {
                return false;
            }
        });
    });
}
 
开发者ID:kikermo,项目名称:Things-Audio-Renderer,代码行数:27,代码来源:RxBroadcastReceiver.java

示例7: getProfile

import io.reactivex.Observable; //导入方法依赖的package包/类
@Override public Observable<RxDto<UserProfile>> getProfile() {
    final DatabaseReference userProfileRef = userRootRef.child(DatabaseNames.USER_PROFILE);
    return Observable.create(emitter -> {
        final ValueEventListener listener =
                userProfileRef.addValueEventListener(new FirebaseValueListener<UserProfile>() {
                    @Override public void onCancelled(DatabaseError databaseError) {
                        emitter.onError(databaseError.toException());
                    }

                    @Override public void onChildChanged(UserProfile userProfile) {
                        emitter.onNext(new RxDto<>(userProfile));
                    }
                });

        emitter.setCancellable(() -> userProfileRef.removeEventListener(listener));
    });
}
 
开发者ID:riteshakya037,项目名称:Subs,代码行数:18,代码来源:FirebaseSessionDataStore.java

示例8: write

import io.reactivex.Observable; //导入方法依赖的package包/类
public static Observable<File> write(Context context, File dir, Playlist playlist) {
    if (!dir.exists()) //noinspection ResultOfMethodCallIgnored
        dir.mkdirs();
    File file = new File(dir, playlist.name.concat("." + EXTENSION));

    //ArrayList<PlaylistSong> songs = PlaylistSongsLoader.getPlaylistSongList(context, playlist.id);
    return Observable.create(e ->
            PlaylistSongsLoader.getPlaylistSongList(context, playlist.id)
                    .subscribe(songs -> {
                        if (songs.size() > 0) {
                            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                            bw.write(HEADER);
                            for (Song song : songs) {
                                bw.newLine();
                                bw.write(ENTRY + song.duration + DURATION_SEPARATOR + song.artistName + " - " + song.title);
                                bw.newLine();
                                bw.write(song.data);
                            }

                            bw.close();
                        }
                        e.onNext(file);
                        e.onComplete();
                    }));
}
 
开发者ID:h4h13,项目名称:RetroMusicPlayer,代码行数:26,代码来源:M3UWriter.java

示例9: suggestSongs

import io.reactivex.Observable; //导入方法依赖的package包/类
public static Observable<ArrayList<Song>> suggestSongs(@NonNull Context context) {
    return Observable.create(observer -> {
        SongLoader.getAllSongs(context).subscribe(songs -> {
            ArrayList<Song> list = new ArrayList<>();
            if (songs.isEmpty()) {
                observer.onNext(new ArrayList<Song>());
                observer.onComplete();
                return;
            }
            ShuffleHelper.makeShuffleList(songs, -1);
            if (songs.size() > 10) {
                list.addAll(songs.subList(0, 10));
            }
            observer.onNext(list);
            observer.onComplete();
        });
    });
}
 
开发者ID:h4h13,项目名称:RetroMusicPlayer,代码行数:19,代码来源:HomeLoader.java

示例10: getMyAttentionList

import io.reactivex.Observable; //导入方法依赖的package包/类
@Override
public Observable<List<MyAttentionEntity>> getMyAttentionList(String userid) {
    return Observable.create((ObservableOnSubscribe<List<MyAttentionEntity>>) emitter -> {

        BmobQuery<MyAttentionEntity> query = new BmobQuery<MyAttentionEntity>();
        query.addWhereEqualTo("userId", userid);
        query.order("-createdAt");
        query.findObjects(new FindListener<MyAttentionEntity>() {
            @Override
            public void done(List<MyAttentionEntity> list, BmobException e) {
                if (list!=null)
                    emitter.onNext(list);
            }
        });
    });
}
 
开发者ID:Zweihui,项目名称:Aurora,代码行数:17,代码来源:AttentionModel.java

示例11: checkPlayServicesAvailable

import io.reactivex.Observable; //导入方法依赖的package包/类
private Observable<Boolean> checkPlayServicesAvailable() {
    return Observable.create(new ObservableOnSubscribe<Boolean>() {
        @Override
        public void subscribe(ObservableEmitter<Boolean> e) throws Exception {
            final Activity activity = activityReference.get();
            if (activity != null) {
                final GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
                final int status = apiAvailability.isGooglePlayServicesAvailable(activity);

                if (status != ConnectionResult.SUCCESS) {
                    e.onError(new PlayServicesNotAvailableException());
                } else {
                    e.onNext(true);
                    e.onComplete();
                }
            }
        }
    });
}
 
开发者ID:florent37,项目名称:RxGps,代码行数:20,代码来源:RxGps.java

示例12: createObservable

import io.reactivex.Observable; //导入方法依赖的package包/类
/**
 * 生成Observable
 *
 * @param t
 * @return Flowable
 */
public static <T> Observable<T> createObservable(final T t) {
    return Observable.create(new ObservableOnSubscribe<T>() {
        @Override
        public void subscribe(ObservableEmitter<T> emitter) throws Exception {
            try {
                emitter.onNext(t);
                emitter.onComplete();
            } catch (Exception e) {
                emitter.onError(e);
            }
        }
    });
}
 
开发者ID:Horrarndoo,项目名称:YiZhi,代码行数:20,代码来源:RxHelper.java

示例13: readFromRealm

import io.reactivex.Observable; //导入方法依赖的package包/类
protected <T> Observable<T> readFromRealm(Callable<T> callable) {
    return Observable.create(e -> {
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        callable.execute(realm, e);
    } finally {
        if (realm != null) {
            realm.commitTransaction();
            realm.close();
        }
    }
    });
}
 
开发者ID:krokers,项目名称:exchange-rates-mvvm,代码行数:16,代码来源:RealmManagerBase.java

示例14: getData

import io.reactivex.Observable; //导入方法依赖的package包/类
@Override
    public Observable<List<Feature>> getData() {
        return Observable.create(new ObservableOnSubscribe<List<Feature>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<Feature>> e) throws Exception {
                List<Feature> features = new ArrayList<>();
                String[] featuresNames = ResUtil.getStringArray(R.array.feature_name);
                Feature feature;
                TypedArray ar = ResUtil.getResources().obtainTypedArray(R.array.feature_logo);
                for (int i = 0; i < ar.length(); i++){
                    int stringResId = ResUtil.getStringId(featuresNames[i]);
//                    String featureName = ResUtil.getString(stringResId);
                    feature = new Feature(i, stringResId, ar.getResourceId(i, 0));
                    features.add(feature);
                }
                ar.recycle();

                /*for (int i = 0; i < featuresNames.length; i++) {
                    int stringResId = ResUtil.getStringId(featuresNames[i]);
                    String featureName = ResUtil.getString(stringResId);
                    String resName = ResUtil.getResName(stringResId);
                    int drawableResId = ResUtil.getDrawableId(resName);
                    feature = new Feature(i, featureName, drawableResId);
                    features.add(feature);
                }*/
                e.onNext(features);
                e.onComplete();
            }
        });
    }
 
开发者ID:fendoudebb,项目名称:PlayAndroid,代码行数:31,代码来源:FeatureRepository.java

示例15: getObservable

import io.reactivex.Observable; //导入方法依赖的package包/类
private Observable<List<Integer>> getObservable() {
  return Observable.create(new ObservableOnSubscribe<List<Integer>>() {
    @Override
    public void subscribe(@NonNull ObservableEmitter<List<Integer>> e) throws Exception {
      e.onNext(Arrays.asList(1, 2, 3, 4, 5));
    }
  });
}
 
开发者ID:GurpreetSK95,项目名称:rx-examples-android,代码行数:9,代码来源:MainActivity.java


注:本文中的io.reactivex.Observable.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。