Android的自动安装的APK的Android、APK

2023-09-07 09:26:46 作者:︻有情有义

我有一个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);
 
精彩推荐
图片推荐