用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.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();
}
}