本文整理汇总了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);
}
}
});
}
示例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();
});
}
示例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();
}
});
}
});
}
示例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));
}
示例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;
}
});
});
}
示例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));
});
}
示例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();
}));
}
示例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();
});
});
}
示例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);
}
});
});
}
示例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();
}
}
}
});
}
示例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);
}
}
});
}
示例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();
}
}
});
}
示例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();
}
});
}
示例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));
}
});
}