我使用AsyncTask强制更新我的应用程序,但似乎在RxJava中执行此操作比使用AsyncTask要好得多。
我试过了,但它不起作用。
这是用于版本检查的异步任务代码
@Override
protected String doInBackground(String... params) {
try {
Document document = Jsoup.connect("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&hl=en")
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
if (document != null) {
Elements element = document.getElementsContainingOwnText("Current Version");
for (Element ele : element) {
if (ele.siblingElements() != null) {
Elements sibElemets = ele.siblingElements();
for (Element sibElemet : sibElemets) {
newVersion = sibElemet.text();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return newVersion;
}
@Override
protected void onPostExecute(String onlineVersion) {
super.onPostExecute(onlineVersion);
if (onlineVersion != null && !onlineVersion.isEmpty()) {
if (Float.valueOf(BuildConfig.VERSION_NAME) < Float.valueOf(onlineVersion)) {
showUpdateDialog();
}
}
}
为了在活动中调用它,我使用了这个
new VersionChecker(this).execute();
这是我尝试过的,但似乎缺少一些东西,因为它不起作用
private void checkVersion() {
Observable<String> observable = Observable.fromCallable(new Callable<String>() {
@Override
public String call() throws Exception {
String data = getHeadlines("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&hl=en");
return data;
}
});
}
public String getHeadlines(String source) throws IOException {
Document document = Jsoup.connect(source)
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
if (document != null) {
Elements element = document.getElementsContainingOwnText("Current Version");
for (Element ele : element) {
if (ele.siblingElements() != null) {
Elements sibElemets = ele.siblingElements();
for (Element sibElemet : sibElemets) {
newVersion = sibElemet.text();
}
}
}
}
return newVersion;
}
我试过这个,它有效!
在新类(例如版本检查器(中插入此
public Observable<String> createObservable() {
//Could use fromCallable
return Observable.defer(new Callable<Observable<String>>() {
@Override
public Observable<String> call() throws Exception {
Observable<String> data = getVersion("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "&hl=en");
return data;
}
});
}
public Observable<String> getVersion(String source) throws IOException {
Document document = Jsoup.connect(source)
.timeout(30000)
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
if (document != null) {
Elements element = document.getElementsContainingOwnText("Current Version");
for (Element ele : element) {
if (ele.siblingElements() != null) {
Elements sibElemets = ele.siblingElements();
for (Element sibElemet : sibElemets) {
newVersion = sibElemet.text();
}
}
}
}
return Observable.just(newVersion);
}
并在您的活动中初始化为版本检查创建的类,并创建新方法来从版本检查器类调用可观察量
private void getCurrentVersion() {
versionChecker.createObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.d("", "onSubscribe: " + d);
}
//on execute
@Override
public void onNext(String onlineVersion) {
if (onlineVersion != null && !onlineVersion.isEmpty()) {
if (Float.valueOf(BuildConfig.VERSION_NAME) < Float.valueOf(onlineVersion)) {
versionChecker.showUpdateDialog();
}
}
}
@Override
public void onError(Throwable e) {
Log.e("", "onError: ", e);
}
@Override
public void onComplete() {
Log.d("TAG", "onComplete: ");
}
});
}