0
我在讀一本名爲「RxJava Essentials」的書。以下代碼來自那裏。我修改了一下。RxJava回調
我在我的Activity
的OnCreate
中調用了refreshList()
。 subscriber.onNext()
已在getApps()
多次調用,subscriber.onCompleted()
已被調用。之後,onNext
callbak(在refreshList()
中執行)已被調用。 public void onNext(List<AppInfo> appInfos)
我徘徊爲什麼onNext()
當調用subscriber.onNext()
時調用不被調用?爲什麼後來被調用?
void refreshList()
{
getApps().toSortedList()
.subscribe(new Observer<List<AppInfo>>()
{
@Override
public void onCompleted() {
Log.e(TAG, "onCompleted()");
}
@Override
public void onError(Throwable te) {
Log.e(TAG, "onError()");
}
@Override
public void onNext(List<AppInfo> appInfos) {
Log.e(TAG, "onNext()");
for (AppInfo tmpInfo : appInfos)
{
//tmpInfo.toString();
Log.e(TAG, String.format("tmpInfo = %s", tmpInfo.toString()));
}
}
});
}
private Observable<AppInfo> getApps()
{
return Observable.create(subscriber ->
{
List<AppInfoRich> apps = new ArrayList<>();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(mainIntent, 0);
for (ResolveInfo info : infos)
{
apps.add(new AppInfoRich(this, info));
}
for (AppInfoRich appInfo : apps)
{
Bitmap icon = BitmapUtils.drawableToBitmap(appInfo.getIcon());
String name = appInfo.getName();
String iconPath = mFilesDir + "/" + name;
//BitmapUtils.storeBitmap(App.instance, icon, name);
BitmapUtils.storeBitmap(getApplicationContext(), icon, name);
if(subscriber.isUnsubscribed())
{
return;
}
subscriber.onNext(new AppInfo(name, iconPath, appInfo.getLastUpdateTime()));
}
if(!subscriber.isUnsubscribed())
{
subscriber.onCompleted();
}
});
}
感謝您的回答。 – MomAndDad