From f595efe355932f25cac8d2998e7bc83145279a0c Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sat, 14 Mar 2020 17:22:06 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8Handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RN/A/VersionUpgrade/UpgradeModule.java | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java b/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java index 27bbb15..118419a 100644 --- a/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java +++ b/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java @@ -8,6 +8,8 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; +import android.os.Handler; +import android.os.Message; import android.provider.Settings; import android.util.Log; @@ -65,28 +67,28 @@ public class UpgradeModule extends ReactContextBaseJavaModule { } } - private void _alertDialog(){ + void _alertDialog(){ final UpgradeModule that = this; AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity()) - .setTitle(TITLE) - .setMessage(CONTENT) - .setNeutralButton("取消", - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - } - ) - .setNegativeButton("更新", - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - that._startDownload(); - } - } - ) - .show(); + .setTitle(TITLE) + .setMessage(CONTENT) + .setNeutralButton("取消", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + } + ) + .setNegativeButton("更新", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + that._startDownload(); + } + } + ) + .show(); dialog.setCanceledOnTouchOutside(true);//可选 dialog.setCancelable(true);//可选 } @@ -119,6 +121,7 @@ public class UpgradeModule extends ReactContextBaseJavaModule { .method("GET", null) .build(); Call call = okHttpClient.newCall(request); + final Handler handler = new ShowUpgradeAlertHandler(that); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { @@ -134,12 +137,12 @@ public class UpgradeModule extends ReactContextBaseJavaModule { Log.d("VERSION CHECK", jsonText); JSONObject json = new JSONObject(jsonText); int status = json.getInt("status"); - CONTENT = json.getString("message"); if(status > 0){ - String version = json.getString("version"); + CONTENT = json.getString("message"); +// String version = json.getString("version"); JSONObject jsonData = json.getJSONObject("data"); DOWNLOAD_URL = jsonData.getString("url"); - that._alertDialog(); + handler.sendEmptyMessage(0); } } catch (IOException e) { Log.e("REQUEST FAILED", "AAA", e); @@ -151,3 +154,17 @@ public class UpgradeModule extends ReactContextBaseJavaModule { }); } } + +final class ShowUpgradeAlertHandler extends Handler +{ + private UpgradeModule module; + + ShowUpgradeAlertHandler(UpgradeModule module) + { + this.module = module; + } + @Override + public void handleMessage(Message message){ + module._alertDialog(); + } +} \ No newline at end of file