强制更新以及进度对话

时间:2018-08-11 13:44:41

标签: android asynchronous android-asynctask androidhttpclient

请任何人帮助我需要更新应用程序强制更新应用程序!强制更新效果很好,每当新版本下次在病房中完成更新时,我都需要在应用程序中创建一个对话框进度栏,例如1-100%,它们不会显示更新按钮!请任何人在这里帮助我

{"Version": [
{
    "version": "1.1",
    "error": "02"
    }
],
"Controle": [
    {
         "version": "1.1",
         "error": "02"
    }
 ]
}


private static  String url = "http://example.json";
 String VersionUpdate;   

 private class VersionCheck extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();


        String jsonStr = sh.makeServiceCall(url);

        if (jsonStr != null){
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray version = jsonObj.getJSONArray("Version");
                for (int i = 0; i < version.length(); i++){

                    JSONObject v = version.getJSONObject(i);

                    VersionUpdate = v.getString("version");
                }



            }catch (final JSONException e) {
                // Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            //Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


        String VersionName = BuildConfig.VERSION_NAME;


        if (VersionUpdate.equals(VersionName)){

            //Do Nothing
        }else {

            android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
            builder.setTitle("New Update Available");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setCancelable(false);
            builder.setMessage("New version available, Click on update to update our latest app")
                    .setPositiveButton("UPDATE", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {



                            try {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.apk")));
                            } catch (android.content.ActivityNotFoundException anfe) {
                                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.apk")));
                            }

                            finish();

                        }
                    });

            android.support.v7.app.AlertDialog alert = builder.create();
            alert.show();


        }


    }
}


class HttpHandler {

private  static  final String TAG = HttpHandler.class.getSimpleName();

public HttpHandler(){


}

public String makeServiceCall(String reqUrl) {
    String response = null;
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}


}

0 个答案:

没有答案