WebView 中的下载连接和非网络协议的连接 – 推酷

给Webview添加一个  wb.setDownloadListener(); 下载监听DownloadListener

实现如下

 

<span class="keyword">public</span> <span class="keyword">void</span> onDownloadStart(String url, String userAgent,</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>String contentDisposition, String mimetype, <span class="keyword">long</span> contentLength){</p><p><span class="indent">  </span><span class="indent">  </span>Context context = getContext();</p><p><span class="indent">  </span><span class="indent">  </span><span class="keyword">if</span>(context==<span class="keyword">null</span>){</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">return</span>;</p><p><span class="indent">  </span><span class="indent">  </span>}</p><p><span class="indent">  </span><span class="indent">  </span>wb.stopLoading();</p><p><span class="indent">  </span><span class="indent">  </span>Uri uri = Uri.parse(url);</p><p><span class="indent">  </span><span class="indent">  </span><span class="keyword">if</span>(Build.VERSION.SDK_INT&gt;=<span class="number">9</span>){<span class="comment">//直接调用系统的下载管理应用下载。使用方法可以查找<span style="font-family: Arial, Helvetica, sans-serif;">DownloadManager 相关</span></span><span style="font-family: Arial, Helvetica, sans-serif;"></span></p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>Request request =  <span class="keyword">new</span> Request(uri);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setMimeType(mimetype);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setDescription(<span class="string">"下载数据"</span>);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>String path = uri.getPath();</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">int</span> indexOf = path.lastIndexOf(<span class="string">"/"</span>);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setDestinationUri(Uri.fromFile(<span class="keyword">new</span> File(Environment.getExternalStorageState(),path.substring(indexOf+<span class="number">1</span>, path.length()))));</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span></p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setTitle(<span class="string">"下载数据"</span>);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">if</span>(Build.VERSION.SDK_INT&gt;=<span class="number">11</span>){</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setAllowedNetworkTypes(Request.NETWORK_WIFI);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>}<span class="keyword">else</span>{</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>request.setShowRunningNotification(<span class="keyword">true</span>);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>}</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>dm.enqueue(request);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">return</span>;</p><p><span class="indent">  </span><span class="indent">  </span>}</p><p>               <span class="comment">// 如果低版本或者不想使用上边方法,可以直接调用外部浏览器</span></p><p><span class="indent">  </span><span class="indent">  </span>Intent intent = <span class="keyword">new</span> Intent(Intent.ACTION_VIEW, uri);</p><p><span class="indent">  </span><span class="indent">  </span>intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());</p><p><span class="indent">  </span><span class="indent">  </span>context.startActivity(intent);</p><p>        </p><p><span class="indent">  </span><span class="indent">  </span></p><p><span class="indent">  </span><span class="indent">  </span></p><p><span class="indent">  </span>}

webview 中的非网络协议连接处理

wb.setWebViewClient(<span class="keyword">new</span> WebViewClient(){</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="annotation">@Override</span></p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">public</span> <span class="keyword">boolean</span> shouldOverrideUrlLoading(WebView view, String url) {</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">if</span>(url!=<span class="keyword">null</span>&amp;&amp;!url.toLowerCase().startsWith(<span class="string">"http"</span>)){</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>Uri uri = Uri.parse(url);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>        Context context = wb.getContext();</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>        Intent intent = <span class="keyword">new</span> Intent(Intent.ACTION_VIEW, uri);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>        context.startActivity(intent);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">return</span> <span class="keyword">true</span>;</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>}</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>wb.loadUrl(url);</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span><span class="keyword">return</span> <span class="keyword">true</span>;</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>}</p><p><span class="indent">  </span><span class="indent">  </span>});

至于连接中的协议可以用现成的,也可以自定义

如果使用现成的,可让服务器端写类似下边这样的

sms://10010?body=给我冲话费    会直接打开发短信界面,并填好内容

也可以自定义协议,与服务器端沟通,连接地址为

mcs://usertail?uid=25564

而activity的配置如下,就可以在点击WebView中超链接时候,打开对应的界面

<span class="tag">&lt;<span class="title">activity</p><p><span class="indent">  </span>android:name=".UserDetailActivity"</p><p><span class="indent">  </span>android:screenOrientation="portrait"</p><p><span class="indent">  </span>android:theme="@style</span>/<span class="attribute">Theme_NoTitle</span>" &gt;</span></p><p><span class="indent">  </span> <span class="tag">&lt;<span class="title">intent-filter</span>&gt;</span></p><p><span class="indent">  </span><span class="indent">  </span><span class="tag">&lt;<span class="title">action</span> <span class="attribute">android:name</span>=<span class="value">"android.intent.action.VIEW"</span> /&gt;</span></p><p><span class="indent">  </span><span class="indent">  </span><span class="tag">&lt;<span class="title">category</span> <span class="attribute">android:name</span>=<span class="value">"android.intent.category.DEFAULT"</span> /&gt;</span></p><p><span class="indent">  </span><span class="indent">  </span><span class="tag">&lt;<span class="title">data</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>android:host="usertail"</p><p><span class="indent">  </span><span class="indent">  </span><span class="indent">  </span>android:scheme="mcs"</span> /&gt;</span></p><p><span class="indent">  </span><span class="tag">&lt;/<span class="title">intent-filter</span>&gt;</span></p><p><span class="tag">&lt;/<span class="title">activity</span>&gt;</span>

来源URL:http://www.tuicool.com/articles/fMvIRv