Web查看使用android下载管理器下载文件

时间:2015-10-16 14:54:49

标签: java android android-webview android-download-manager

我一直在研究一个使用Web视图查看网站网页的Android应用程序。但是这个网站有视频和音频文件。每当用户点击视频/音频的网站下载按钮时,它就会打开手机的默认Android浏览器。我希望我的应用程序能够处理下载本身,我研究了Android的下载管理器,但无法让它工作。如何实现下载管理器来监听网站的下载点击并处理下载,将其保存到指定的目录而不需要拨打手机浏览器?

这是我的具有网络视图的活动的代码,编辑我要回答的代码将更为可取。

    package com.akinlawongroup.JWBroadcastApp;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.NavUtils;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.WebSettings;
    import android.webkit.WebView;

    public class DrawerActivity1 extends AppCompatActivity {

        private WebView mWebView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity1);
            Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
            setSupportActionBar(toolbar);

            getSupportActionBar().setDisplayShowHomeEnabled(true);
            NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
                    getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
            drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

            mWebView = (WebView) findViewById(R.id.webView);
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            mWebView.loadUrl("http://tv.jw.org/#en/video");


    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            Log.d("WEB_VIEW_TEST", "error code:" + errorCode + " - " + description);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // handle different requests for different type of files
            // this example handles downloads requests for .apk and .mp3 files
            // everything else the webview can handle normally
            if (url.endsWith(".mp4")) {
                Uri source = Uri.parse(url);
                // Make a new request pointing to the .apk url
                DownloadManager.Request request = new DownloadManager.Request(source);
                // appears the same in Notification bar while downloading
                request.setDescription("Description for the DownloadManager Bar");
                request.setTitle("JWVideo.mp4");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                }
                // save the file in the "Downloads" folder of SDCARD
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "JWVideo.mp4");
                // get download service and enqueue file
                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                manager.enqueue(request);
            }
            // if there is a link to anything else than .apk or .mp3 load the URL in the webview
            else view.loadUrl(url);
            return true;
        }
    });
}



            // getSupportActionBar().setHomeButtonEnabled(true);
            // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_sub, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            // if (id==R.id.navigate){
            //     startActivity(new Intent(this, SubActivity.class));
            // }

            return super.onOptionsItemSelected(item);
        }
    }

1 个答案:

答案 0 :(得分:0)

要下载,请尝试:

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

希望它有所帮助!