本文整理匯總了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));
}
});
}