Android 自动安装 APK [英] Android auto installation of APKs

查看:32
本文介绍了Android 自动安装 APK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 webview,它基本上能够拦截各种链接、视频、apk、href.

I have a webview which basically is capable of intercepting all sorts of links, video, apks, hrefs.

现在,我想要的是一旦我从一个 url 下载了一个 APK,它就会被自动安装:

Now, what I want is once I download an APK from a url, that it'll be auto installed:

这是shouldOverrideUrlLoading()代码的一部分:

        else if(url.endsWith(".apk")) 
        {
        mWebView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(final String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {   
                    }
                    });
        Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
        startActivity(intent);  
        return true;

如果我添加

intent.setDataAndType(Uri.parse(url), "application/vnd.android.package-archive");

比应用程序崩溃...

有什么想法可以做什么吗?

Any ideas as to what to do?

我能够自动启动下载和安装包(使用 sleep() ):

I was able to initiate a download and an installation of the package automatically (using a sleep() ):

        else if(url.endsWith(".apk")) 
        {
        mWebView.setDownloadListener(new DownloadListener() {
                    public void onDownloadStart(final String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {   
                    }
                    });
        Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url));
        startActivity(intent); 
        String fileName = Environment.getExternalStorageDirectory() + "/download/" + url.substring( url.lastIndexOf('/')+1, url.length() );
        install(fileName);
        return true;

而且,正如维他命所建议的:

and, as vitamoe suggested:

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);
}

但是,我无法捕获下载完成的确切时间,可能需要创建自己的下载功能而不是使用浏览器的功能,有什么想法吗?

However, I'm unable to capture the exact time that the download is finished, might need to create my own download function and not use the browser's one, any ideas?

推荐答案

要下载文件而不用浏览器做某事.像这样:

To download a file without the browser do sth. like this:

String apkurl = "http://your.url.apk";
InputStream is;
try {
    URL url = new URL(apkurl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoOutput(true);
    con.connect();
    is = con.getInputStream();
} catch (SSLException e) {
    // HTTPS can end in SSLException "Not trusted server certificate"
}

// Path and File where to download the APK
String path = Environment.getExternalStorageDirectory() + "/download/";
String fileName = apkurl.substring(apkurl.lastIndexOf('/') + 1);
File dir = new File(path);
dir.mkdirs(); // creates the download directory if not exist
File outputFile = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);

// Save file from URL to download directory on external storage
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
    fos.write(buffer, 0, len);
}
fos.close();
is.close();

// finally, install the downloaded file
install(path + fileName);

这篇关于Android 自动安装 APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆