并开始安装提示里面的Andr​​oid应用程序

并开始安装提示里面的Andr​​oid应用程序

本文介绍了下载的APK,并开始安装提示里面的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个民营企业的Andr​​oid应用程序。我的目标是检测当有可用的更新,并提供用户下载。如果用户选择下载更新文件下载和Android应用安装提示显示。

我成功检查更新,问题是apk文件没有下载(创建空文件),因此有分析包有问题。错误是在Android应用显示安装提示。

code:

 公共无效downloadfileto(字符串fileurl,字符串文件名){
    字符串的myString;
    尝试{
            FileOutputStream中F =新的FileOutputStream(文件名);
            尝试{
                    网址URL =新的URL(fileurl);
                    URLConnection的urlConn = url.openConnection();
                    InputStream为= urlConn.getInputStream();
                    二BufferedInputStream为=新的BufferedInputStream(是,8000);
                    INT电流= 0;
                    而((电流= bis.read())!= - 1){
                            f.write((字节)电流);
                    }
            }赶上(例外五){
                    myString的= e.getMessage();
            }
            f.flush();
            f.close();
            安装(文件名);
    }赶上(FileNotFoundException异常五){
            e.printStackTrace();
    }赶上(IOException异常五){
            e.printStackTrace();
    }
}保护无效安装(字符串文件名){
    意图安装=新意图(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(新文件(文件名)),
            应用程序/ vnd.android.package归档);
    startActivity(安装);
}

功能downloadfileto被调用:

  downloadfileto(HTTP://some-url/ind.apk,数据/数据​​/ my.package.name / app.apk);


解决方案

我要感谢大家的帮助在这里。我通过计数与Web视图下载,下载检测,下载路径服务器打开php脚本和启动活动安装的应用程序解决了这个问题。

文件的名称是始终在形式上IND-version.apk(例如:IND-1-0.apk),也因为我得到新的更新版本号,当我检查更新,我决定把它放在与群众演员用它来确定的文件名。

code:

 的WebView myWebView =(的WebView)findViewById(R.id.helpview);
    的ShowDialog();
    myWebView.setWebViewClient(新WebViewClient());    myWebView.loadUrl(URL);
    myWebView.getSettings()setJavaScriptEnabled(假)。    myWebView.setWebViewClient(新WebViewClient(){
         @覆盖
         公共无效onPageFinished(的WebView视图,字符串URL){
             super.onPageFinished(查看,网址);
             dismissDialog();
         }
    });    myWebView.setDownloadListener(新DownloadListener(){
        公共无效onDownloadStart(URL字符串,字符串的userAgent,
                字符串contentDisposition,字符串MIME类型,
                长CONTENTLENGTH){
            意向意图=新意图(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(URL));
            startActivity(意向);            捆绑额外= getIntent()getExtras()。
            串V = extras.getString(V);
            V = v.replace( - ,。);
            Log.i(文件,V);            文件LOC = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            Log.i(文件,loc.toString()+/ ind-+ V +.apk文件);            安装(loc.toString()+/ ind-+ V +.apk文件);
        }
    });

和安装:

 保护无效安装(字符串文件名){
    意图安装=新意图(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(新文件(文件名)),
            应用程序/ vnd.android.package归档);
    startActivity(安装);
}

I'm building a private enterprise Android application. My goal is to detect when there is update available and offer user to download it. If user choose to download update file is downloaded and android app install prompt is showed.

I successfully check for update, the problem is that apk file is not downloaded (empty file is created) therefore "There is a problem parsing the package." error is showed in android app install prompt.

Code:

public void downloadfileto(String fileurl, String filename) {
    String myString;
    try {
            FileOutputStream f = new FileOutputStream(filename);
            try {
                    URL url = new URL(fileurl);
                    URLConnection urlConn = url.openConnection();
                    InputStream is = urlConn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, 8000);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            f.write((byte) current);
                    }
            } catch (Exception e) {
                    myString = e.getMessage();
            }
            f.flush();
            f.close();
            install(filename);
    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
}

protected void install(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}

Function downloadfileto is called with:

downloadfileto("http://some-url/ind.apk", "data/data/my.package.name/app.apk");
解决方案

I would like to thank you all for helping here. I solved the problem by opening php script on server that counts downloads with web view, detecting download, path of download and starting activity to install application.

Name of file is always in form "ind-version.apk" (Example: ind-1-0.apk) and because I get version number of new update when I check for updates I decided to put it in extras and use it to determine file name.

Code:

    WebView myWebView = (WebView) findViewById(R.id.helpview);
    showDialog();
    myWebView.setWebViewClient(new WebViewClient());

    myWebView.loadUrl(url);
    myWebView.getSettings().setJavaScriptEnabled(false);

    myWebView.setWebViewClient(new WebViewClient() {
         @Override
         public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
             dismissDialog();
         }
    });

    myWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);

            Bundle extras = getIntent().getExtras();
            String v = extras.getString("v");
            v = v.replace(".", "-");
            Log.i("File", v);

            File loc = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            Log.i("File", loc.toString() + "/ind-" + v + ".apk");

            install(loc.toString() + "/ind-" + v + ".apk");
        }
    });

And install:

protected void install(String fileName) {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}

这篇关于下载的APK,并开始安装提示里面的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:52