在Android应用中添加检查更新功能

时间:2016-05-14 05:20:08

标签: android

我想在Android应用程序中添加检查更新功能

我知道已经问过question并在Github上发布了一个lib  How to allow users to check for the latest app version from inside the app?

但由于版权问题,我无法在Google Play商店发帖

所以我不能使用方法

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您的代码每天检查一次更新。仅在您一天打开一次应用程序时才有效。在你的启动器活动的onCreate()中。

SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences
                (Dashboard.this);
        long lastUpdateTime = mSettings.getLong("lastUpdateTime", 0);
        /* Should Activity Check for Updates Now? */
        if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {

        /* Save current timestamp for next Check*/
            SharedPreferences.Editor editor = mSettings.edit();
            lastUpdateTime = System.currentTimeMillis();
            editor.putLong("lastUpdateTime", lastUpdateTime);
            editor.commit();

        /* Start Update by using asynctask that run in backGround eg. http://portal.aksuniversity.com:8089/utility/apkversion*/
            String URL = "your app url in my case ;
            AsyncRequest asyncRequestTime = new AsyncRequest(Dashboard.this, "GET", null,
                    null, 3);
            asyncRequestTime.execute(URL);
        }

你将获得的响应是​​json对象。获取版本代码的jsonObject密钥,并将其与您的应用版本代码

相匹配
 int versionCode = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0).versionCode;
            if (versionCode < jsonObject.getString(""VersionCode)) 
//if greater show dialog for app update and download apk with new url of your app which will give you apk

下载,您可以使用

private String callApiDownload(String address) {

        try {
            URL url = new URL(address);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            if (Cookie.getCookie() != null)
                urlConnection.addRequestProperty("Cookie", Cookie.getCookie());

            if (urlConnection.getResponseCode() == 200) {

                File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
                boolean fileExist = true;
                if (!file.exists()) {
                    fileExist = file.mkdir();
                }
                if (fileExist) {
                    String FileName = Environment.getExternalStorageDirectory() + "/" + folderName + "/"
                            + fileName;
                    InputStream inputStream = urlConnection.getInputStream();
                    FileOutputStream outputStream = new FileOutputStream(FileName);

                    int bytesRead;
                    byte[] buffer = new byte[4096];
                    int total = 0;
                    int contentLength = urlConnection.getContentLength();

                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
                            publishProgress((int) ((total * 100) / contentLength));
                        }
                    }
                    outputStream.flush();
                    outputStream.close();
                    inputStream.close();
                    return fileName;
                }
            } else {
                InputStream inputStream = new BufferedInputStream(urlConnection.getErrorStream());
                return fileName = Connection.convertInputStreamToString(inputStream);
            }
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return fileName;
    }

在下载完成后,您可以使用意图打开apk进行安装,这将提示用户进行新的更新apk安装。

第一个代码会在用户打开应用程序时每天调用一次api。此api将返回您的应用程序版本代码和版本名称的json对象。您需要在为用户创建新apk时手动维护它。解析json对象。并获取版本代码。在您的应用中检查api版本代码是否大于您的应用版本代码。如果它是一个新的api,这是我给出的最后一个代码从url下载你的apk(在服务器你必须放置apk为指定的urlby调用你的apk将下载)这是你需要运行的最后一个代码在反向的asynctak中。我会更正确地创建源代码并通知你。