You've already forked RN_Android_VersionUpgrade
							
							就这样吧
This commit is contained in:
		@@ -20,7 +20,10 @@ import com.facebook.react.bridge.ReactMethod;
 | 
				
			|||||||
import org.json.JSONException;
 | 
					import org.json.JSONException;
 | 
				
			||||||
import org.json.JSONObject;
 | 
					import org.json.JSONObject;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.io.InputStream;
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -33,7 +36,7 @@ import okhttp3.Response;
 | 
				
			|||||||
public class UpgradeModule extends ReactContextBaseJavaModule {
 | 
					public class UpgradeModule extends ReactContextBaseJavaModule {
 | 
				
			||||||
    private final ReactApplicationContext reactContext;
 | 
					    private final ReactApplicationContext reactContext;
 | 
				
			||||||
    private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk";
 | 
					    private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk";
 | 
				
			||||||
    private String HOST = "http://luntan.qgmzbxs.com/";
 | 
					    private String HOST = "http://www.tsgzvore.com/";
 | 
				
			||||||
    private String TITLE = "更新";
 | 
					    private String TITLE = "更新";
 | 
				
			||||||
    private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新,让我们一起成为一天更新八次的暴躁老哥吧";
 | 
					    private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新,让我们一起成为一天更新八次的暴躁老哥吧";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -68,41 +71,80 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void _startDownload(){
 | 
					    private void _startDownload() {
 | 
				
			||||||
        OkHttpClient okHttpClient = new OkHttpClient();
 | 
					        OkHttpClient okHttpClient = new OkHttpClient();
 | 
				
			||||||
        final Request request = new Request.Builder()
 | 
					        final Request request = new Request.Builder()
 | 
				
			||||||
                .url(DOWNLOAD_URL)
 | 
					                .url(DOWNLOAD_URL)
 | 
				
			||||||
                .method("GET", null)
 | 
					                .method("GET", null)
 | 
				
			||||||
                .build();
 | 
					                .build();
 | 
				
			||||||
        Call call = okHttpClient.newCall(request);
 | 
					        Call call = okHttpClient.newCall(request);
 | 
				
			||||||
 | 
					        final Handler handler = new DoInstallHandler(this);
 | 
				
			||||||
 | 
					        call.enqueue(new Callback() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onFailure(Call call, IOException e) {
 | 
				
			||||||
 | 
					                Log.e("PACKAGE DOWNLOAD", call.toString(), e);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onResponse(Call call, Response response) {
 | 
				
			||||||
 | 
					                if (response.isSuccessful()) {
 | 
				
			||||||
 | 
					                    if (response.body() == null) return;
 | 
				
			||||||
 | 
					                    FileOutputStream fileOutputStream = null;
 | 
				
			||||||
 | 
					                    InputStream stream = null;
 | 
				
			||||||
 | 
					                    try {
 | 
				
			||||||
 | 
					                        stream = response.body().byteStream();
 | 
				
			||||||
 | 
					                        byte[] buffer = new byte[2048];
 | 
				
			||||||
 | 
					                        int len = 0;
 | 
				
			||||||
 | 
					                        long total = response.body().contentLength();
 | 
				
			||||||
 | 
					                        File file = new File("update.apk");
 | 
				
			||||||
 | 
					                        fileOutputStream = new FileOutputStream(file);
 | 
				
			||||||
 | 
					                        long sum = 0;
 | 
				
			||||||
 | 
					                        while ((len = stream.read(buffer)) != -1) {
 | 
				
			||||||
 | 
					                            fileOutputStream.write(buffer, 0, len);
 | 
				
			||||||
 | 
					                            sum += len;
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        fileOutputStream.flush();
 | 
				
			||||||
 | 
					                    } catch (IOException e) {
 | 
				
			||||||
 | 
					                        Log.e("REQUEST FAILED", "AAA", e);
 | 
				
			||||||
 | 
					                    } finally {
 | 
				
			||||||
 | 
					                        try {
 | 
				
			||||||
 | 
					                            if (fileOutputStream != null) fileOutputStream.close();
 | 
				
			||||||
 | 
					                            if (stream != null) stream.close();
 | 
				
			||||||
 | 
					                        } catch (IOException e) {
 | 
				
			||||||
 | 
					                            e.printStackTrace();
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @ReactMethod
 | 
					    @ReactMethod
 | 
				
			||||||
    public void checkUpgrade(){
 | 
					    public void checkUpgrade() {
 | 
				
			||||||
        OkHttpClient okHttpClient = new OkHttpClient();
 | 
					        OkHttpClient okHttpClient = new OkHttpClient();
 | 
				
			||||||
        String upgradeUrl = HOST.concat("versionCheck");
 | 
					        String upgradeUrl = HOST.concat("versionCheck");
 | 
				
			||||||
        PackageManager manager = reactContext.getPackageManager();
 | 
					        PackageManager manager = reactContext.getPackageManager();
 | 
				
			||||||
@@ -133,14 +175,14 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            @Override
 | 
					            @Override
 | 
				
			||||||
            public void onResponse(Call call, Response response) {
 | 
					            public void onResponse(Call call, Response response) {
 | 
				
			||||||
                if(response.isSuccessful()){
 | 
					                if (response.isSuccessful()) {
 | 
				
			||||||
                    if(response.body() == null) return;
 | 
					                    if (response.body() == null) return;
 | 
				
			||||||
                    try {
 | 
					                    try {
 | 
				
			||||||
                        String jsonText = response.body().string();
 | 
					                        String jsonText = response.body().string();
 | 
				
			||||||
                        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");
 | 
				
			||||||
                        if(status > 0){
 | 
					                        if (status > 0) {
 | 
				
			||||||
                            CONTENT = json.getString("message");
 | 
					                            CONTENT = json.getString("message");
 | 
				
			||||||
//                            String version = json.getString("version");
 | 
					//                            String version = json.getString("version");
 | 
				
			||||||
                            JSONObject jsonData = json.getJSONObject("data");
 | 
					                            JSONObject jsonData = json.getJSONObject("data");
 | 
				
			||||||
@@ -158,16 +200,28 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
final class ShowUpgradeAlertHandler extends Handler
 | 
					final class ShowUpgradeAlertHandler extends Handler {
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    private UpgradeModule module;
 | 
					    private UpgradeModule module;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ShowUpgradeAlertHandler(UpgradeModule module)
 | 
					    ShowUpgradeAlertHandler(UpgradeModule module) {
 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        this.module = module;
 | 
					        this.module = module;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void handleMessage(Message message){
 | 
					    public void handleMessage(Message message) {
 | 
				
			||||||
 | 
					        module._alertDialog();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					final class DoInstallHandler extends Handler {
 | 
				
			||||||
 | 
					    private UpgradeModule module;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    DoInstallHandler(UpgradeModule module) {
 | 
				
			||||||
 | 
					        this.module = module;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public void handleMessage(Message message) {
 | 
				
			||||||
        module._alertDialog();
 | 
					        module._alertDialog();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user