用Handler

This commit is contained in:
Jerry Yan 2020-03-14 17:22:06 +08:00
parent bcca3fbf1d
commit f595efe355

View File

@ -8,6 +8,8 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
@ -65,28 +67,28 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
} }
} }
private void _alertDialog(){ void _alertDialog(){
final UpgradeModule that = this; final UpgradeModule that = this;
AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity()) AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity())
.setTitle(TITLE) .setTitle(TITLE)
.setMessage(CONTENT) .setMessage(CONTENT)
.setNeutralButton("取消", .setNeutralButton("取消",
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); dialog.dismiss();
} }
} }
) )
.setNegativeButton("更新", .setNegativeButton("更新",
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
that._startDownload(); that._startDownload();
} }
} }
) )
.show(); .show();
dialog.setCanceledOnTouchOutside(true);//可选 dialog.setCanceledOnTouchOutside(true);//可选
dialog.setCancelable(true);//可选 dialog.setCancelable(true);//可选
} }
@ -119,6 +121,7 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
.method("GET", null) .method("GET", null)
.build(); .build();
Call call = okHttpClient.newCall(request); Call call = okHttpClient.newCall(request);
final Handler handler = new ShowUpgradeAlertHandler(that);
call.enqueue(new Callback() { call.enqueue(new Callback() {
@Override @Override
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
@ -134,12 +137,12 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
Log.d("VERSION CHECK", jsonText); Log.d("VERSION CHECK", jsonText);
JSONObject json = new JSONObject(jsonText); JSONObject json = new JSONObject(jsonText);
int status = json.getInt("status"); int status = json.getInt("status");
CONTENT = json.getString("message");
if(status > 0){ if(status > 0){
String version = json.getString("version"); CONTENT = json.getString("message");
// String version = json.getString("version");
JSONObject jsonData = json.getJSONObject("data"); JSONObject jsonData = json.getJSONObject("data");
DOWNLOAD_URL = jsonData.getString("url"); DOWNLOAD_URL = jsonData.getString("url");
that._alertDialog(); handler.sendEmptyMessage(0);
} }
} catch (IOException e) { } catch (IOException e) {
Log.e("REQUEST FAILED", "AAA", 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();
}
}