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

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

问题描述

我有一个web视图,基本上是能​​够拦截各种链接,视频,的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() code部分:

This is part of the shouldOverrideUrlLoading() code:

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

而不是应用程序崩溃...

Than the application crashes...

任何想法,该怎么办?

编辑:我是能够自动启动下载和安装包(使用睡眠()):

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;

和,为vitamoe建议:

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天全站免登陆