2015-10-16 64 views
0

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

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

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

当前没有任何代码显示您已经尝试过的内容,以阻止用户与Web视图的交互。也许如果你能展示你已经尝试过的社区将能够帮助更多 – Flexicoder

+0

@Flexicoder我刚刚编辑了问题和代码。但该应用程序仍然在点击下载时崩溃。我错过了什么。 –

回答

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

希望它能帮助!

相关问题