webView链接进入下载页面

如下图所示,在手机浏览器中访问京东的手机版网站(m.jd.com),顶部会有一个广告图,点击这个广告图,如果手机上已经安装了京东App,则直接打开,如果没有安装,则开始下载。

实现方式

1.为Android应用的启动Activity设置一个Schema,如下:

<data android:host="splash" android:scheme="cundong"/>

2.用户点击浏览器中的链接时,在动态创建一个不可见的iframe,并且让这个iframe去加载步骤1中的Schema,如下:

var ifr = document.createElement('iframe');
ifr.src="cundong://splash"

3,如果在指定的时间内没有跳转成功,则当前页跳转到apk的下载地址(或下载页),如下:

window.location = download_url;

HTML代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<! doctype  html>
< html >
     < head >
         < meta  http-equiv = "Content-Type"  content = "text/html; charset=utf-8" >
 
         < meta  name = "apple-mobile-web-app-capable"  content = "yes" >
         < meta  name = "apple-mobile-web-app-status-bar-style"  content = "black" />
 
         < title >this's a demo</ title >
         < meta  id = "viewport"  name = "viewport"  content = "width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui" >
     </ head >
     < body >
         < div >
             < a  id = "J-call-app"  href = "javascript:;"  class = "label" >立即打开&gt;&gt;</ a >
             < input  id = "J-download-app"  type = "hidden"  name = "storeurl"  value = "http://m.chanyouji.cn/apk/chanyouji-2.2.0.apk" >
         </ div >
 
         < script >
             (function(){
                 var ua = navigator.userAgent.toLowerCase();
                 var t;
                 var config = {
                     /*scheme:必须*/
                     scheme_IOS: 'cundong://',
                     scheme_Adr: 'cundong://splash',
                     download_url: document.getElementById('J-download-app').value,
                     timeout: 600
                 };
 
                 function openclient() {
                     var startTime = Date.now();
 
                     var ifr = document.createElement('iframe');
 
 
                     ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr;
                     ifr.style.display = 'none';
                     document.body.appendChild(ifr);
 
                     var t = setTimeout(function() {
                         var endTime = Date.now();
 
                         if (!startTime || endTime - startTime < config.timeout + 200) { 
                             window.location = config.download_url;
                         } else {
                             
                         }
                     }, config.timeout);
 
                     window.onblur = function() {
                         clearTimeout(t);
                     }
                 }
                 window.addEventListener("DOMContentLoaded", function(){
                     document.getElementById("J-call-app").addEventListener('click',openclient,false);
 
                 }, false);
             })()
         </ script >
     </ body >
</ html >

AndroidMainfext.xml

<activity
     android:name=".activity.LauncherActivity"
     android:configChanges="orientation|keyboardHidden|navigation|screenSize"
     android:label="@string/app_name"
     android:screenOrientation="portrait" >
        <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:host="splash" android:scheme="cundong" />
       </intent-filter>
</activity>



转:http://my.oschina.net/liucundong/blog/354029

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于在 WebView 中进行下载操作,您可以使用以下步骤: 1. 配置权限:确保在 AndroidManifest.xml 文件中添加网络权限(`<uses-permission android:name="android.permission.INTERNET" />`)和文件写入权限(`<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />`)。 2. 创建下载任务:在 WebViewClient 的 `shouldOverrideUrlLoading` 方法中拦截下载链接,并创建下载任务。您可以使用 Android 的 DownloadManager 类来处理下载任务。示例代码如下: ```java private DownloadManager downloadManager; // ... WebView webView = findViewById(R.id.webView); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { // 判断是否是要下载链接 if (request.getUrl().toString().endsWith(".apk")) { // 创建下载请求 DownloadManager.Request downloadRequest = new DownloadManager.Request(request.getUrl()); downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); downloadRequest.setTitle("文件下载"); downloadRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); // 设置文件保存路径和名称 String fileName = URLUtil.guessFileName(request.getUrl().toString(), null, null); downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); // 获取下载管理器并开始下载 downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); downloadManager.enqueue(downloadRequest); return true; // 返回 true 表示拦截该链接,不加载页面 } else { return super.shouldOverrideUrlLoading(view, request); } } }); ``` 3. 监听下载完成事件:如果您需要监听下载完成的事件,可以注册广播接收器来接收下载完成的通知。示例代码如下: ```java private BroadcastReceiver downloadReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); // 根据下载 ID 获取下载信息 DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(downloadId); Cursor cursor = downloadManager.query(query); if (cursor.moveToFirst()) { int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); int status = cursor.getInt(statusIndex); if (status == DownloadManager.STATUS_SUCCESSFUL) { // 下载成功,获取文件保存路径 int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); String downloadedFilePath = cursor.getString(uriIndex); // 在这里进行文件操作,例如安装应用或打开文件等 } else if (status == DownloadManager.STATUS_FAILED) { // 下载失败,可以进行相应处理 } } cursor.close(); } }; // 注册广播接收器 registerReceiver(downloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); ``` 以上是一个基本的 WebView 下载的实现过程,您可以根据您的具体需求进行相应的修改和扩展。希望对您有所帮助!如果您有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值