1. 程式人生 > >Webview下載apk

Webview下載apk

 方法一:使用手機內建的瀏覽器下載

webView設定DownloadListener監聽
webView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(String s, String s1, String s2, String s3, long l) {
      //download
   downLoad(context);
    }
});
private void downLoad(Context context) {
    Intent intent = new Intent();
    intent.setAction("android.intent.action.VIEW");
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    Uri contentUri = Uri.parse(url);
    intent.setData(contentUri);
    if (hasbrowser(content, intent)) {
        intent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    }
    context.startActivity(intent);
}
private boolean hasbrowser(Context context, Intent intent) {
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolve : list) {
        if (resolve.activityInfo.packageName.contains("com.android.browser")) {
            return true;
        }
    }
    return false;
}