找到你要的答案

Q:Parsing error on downloaded apk for autoupdate

Q:解析错误下载APK进行自动更新

I want to implement autoupdate for my app.

I used the DownloadManager (and now an AsyncTask for download) and install the file.

The download is working fine. On the PostExecute I fire an intent to install the new apk. Everytime i got a parsing error. When I open the file in ES File Explorer, I am able to install it successfully, but not within the app and the intent.

I even changed from /Android/data/packagename/files to /Download but still not working.

PS: i know the code is dirty, but working, and I changed so often so many thinks to get it work, but it doesnt...

public class UpdateAsyncTask extends AsyncTask<String, Integer, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(LoginActivity.this);
            mProgressDialog.setMessage("A message");
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            // if we get here, length is known, now set indeterminate to false
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setMax(100);
            mProgressDialog.setProgress(progress[0]);
        }

        protected String doInBackground(String... arg0) {
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            try {
                    //URL url = new URL(arg0[0]);
                    URL url = new URL(apkUrl);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
                    // expect HTTP 200 OK, so we don't mistakenly save error report
                    // instead of the file
                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
                    }
                    // this will be useful to display download percentage
                    // might be -1: server did not report the length
                    int fileLength = connection.getContentLength();

                    // download the file
                    input = connection.getInputStream();
                    //output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "Straffv2.apk");
                    output = new FileOutputStream("/storage/emulated/0/Download/Straffv2.apk");
                    File outputFile = new File(Environment.DIRECTORY_DOWNLOADS, "Straffv2.apk");
                    outputFile.setReadable(true, false);
                    byte data[] = new byte[4096];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
                        if (isCancelled()) {
                            input.close();
                            return null;
                        }
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0) // only if total length is known
                            publishProgress((int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }
                } catch (Exception e) {
                    return e.toString();
                } finally {
                    try {
                        if (output != null)
                            output.close();
                        if (input != null)
                            input.close();
                    } catch (IOException ignored) {
                    }

                    if (connection != null)
                        connection.disconnect();
                }
                return null;
        }

        protected void onPostExecute(String result){
             mProgressDialog.dismiss();
                if (result != null) {
                    //Toast.makeText(LoginActivity.this,"Download error: "+result, Toast.LENGTH_LONG).show();
                }
                    else {
                    //Toast.makeText(LoginActivity.this,"File downloaded", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS, "Straffv2.apk")), "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
                    LoginActivity.this.startActivity(intent);           
                }


    }

}

我想为我的应用程序自动更新的实现。

我用downloadmanager(现在下载AsyncTask)和安装文件。

The download is working fine. On the PostExecute I fire an intent to install the new apk. Everytime i got a parsing error. When I open the file in ES File Explorer, I am able to install it successfully, but not within the app and the intent.

我甚至从/ Android /数据/软件/文件/下载但仍然没有工作。

我知道代码是脏的,但工作,我经常改变这么多人想得到它的工作,但它不…

public class UpdateAsyncTask extends AsyncTask<String, Integer, String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mProgressDialog = new ProgressDialog(LoginActivity.this);
            mProgressDialog.setMessage("A message");
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            // if we get here, length is known, now set indeterminate to false
            mProgressDialog.setIndeterminate(false);
            mProgressDialog.setMax(100);
            mProgressDialog.setProgress(progress[0]);
        }

        protected String doInBackground(String... arg0) {
            InputStream input = null;
            OutputStream output = null;
            HttpURLConnection connection = null;
            try {
                    //URL url = new URL(arg0[0]);
                    URL url = new URL(apkUrl);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();
                    // expect HTTP 200 OK, so we don't mistakenly save error report
                    // instead of the file
                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage();
                    }
                    // this will be useful to display download percentage
                    // might be -1: server did not report the length
                    int fileLength = connection.getContentLength();

                    // download the file
                    input = connection.getInputStream();
                    //output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "Straffv2.apk");
                    output = new FileOutputStream("/storage/emulated/0/Download/Straffv2.apk");
                    File outputFile = new File(Environment.DIRECTORY_DOWNLOADS, "Straffv2.apk");
                    outputFile.setReadable(true, false);
                    byte data[] = new byte[4096];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
                        if (isCancelled()) {
                            input.close();
                            return null;
                        }
                        total += count;
                        // publishing the progress....
                        if (fileLength > 0) // only if total length is known
                            publishProgress((int) (total * 100 / fileLength));
                        output.write(data, 0, count);
                    }
                } catch (Exception e) {
                    return e.toString();
                } finally {
                    try {
                        if (output != null)
                            output.close();
                        if (input != null)
                            input.close();
                    } catch (IOException ignored) {
                    }

                    if (connection != null)
                        connection.disconnect();
                }
                return null;
        }

        protected void onPostExecute(String result){
             mProgressDialog.dismiss();
                if (result != null) {
                    //Toast.makeText(LoginActivity.this,"Download error: "+result, Toast.LENGTH_LONG).show();
                }
                    else {
                    //Toast.makeText(LoginActivity.this,"File downloaded", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS, "Straffv2.apk")), "application/vnd.android.package-archive");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
                    LoginActivity.this.startActivity(intent);           
                }


    }

}
android  parsing  apk