0571-89265925
您的当前位置: > 杭楷观点 > 小白知道 >

基于webview/UIwebView的app移动网络下,彻底清除运营商标志、广告和域名劫持广告

时间:2017-06-06

有些人开发了一个基于html5的app,开发好了,一般会遇到以下问题。
1.在中国移动网络下,右下角多出来一个圆形的中国移动运营商标志(本来是一件好事,但是移动网络下,用户手机网速和流量已经紧缺的情况下,居然还要加载这种耗流量的东东,简直太反人类设计了)。
2.在wifi网络下,页面的底部会偶发性得多出来长条广告,严重影响页面的美观和性能(这种情况下应该属于运营商域名劫持或ip劫持)。

现在,杭谐科技的技术为大家提供一种非常好的方法,能够彻底清除这种恶瘤。还用户一个清洁的上网环境。这里就说一下android版本的方法(ios方法同理)


实现原理:由于中国移动标识和其他的广告都直接会自动在页面填写js文件,所以这里直接判断url是否包含js文件,然后再判断url是否是本app授权的域名,如果没有授权说明是其他的外部网址,则过滤掉,并在sdcard里面创建这个js文件,这样就无需关注具体是哪个js文件,反正不是本站授权的文件统统过滤掉。关键是public WebResourceResponse shouldInterceptRequest(WebView view,String url)这个方法,就是截取请求的url然后替换本地的url。当然,你也可以用这个方法过滤:jpg、png、css等等。反正后期服务商该后缀名了,你添加一下类型就好

webView.setWebViewClient(new WebViewClient(){
@SuppressLint ("NewApi")
       @Override  
       public WebResourceResponse shouldInterceptRequest(WebView view,String url) {
           if(url.contains(".js")){
        if(!url.contains("hzappkf.com")){
        String result = url.substring(url.lastIndexOf('/')+1);
        Tools.writeToFile("",getApplicationContext(), result);
        return getWebResourceResponse_sdcard(result);  
        }
        }
           return super.shouldInterceptRequest(view, url);  
       } 
}
public static void writeToFile(String data,Context context,String fileName) {
 
 if(!fileExists(context,fileName)){
 try {
       OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(fileName, Context.MODE_PRIVATE));
       outputStreamWriter.write(data);
       outputStreamWriter.close();
   }
   catch (IOException e) {
       
   } 
 }
}


 
@SuppressLint("NewApi")  
private WebResourceResponse getWebResourceResponse_sdcard(String fileName){  
WebResourceResponse res = new WebResourceResponse("text/javascript", "UTF-8", Tools.readFromFile(getApplicationContext(), fileName));  
   return res; 
}


 
 
 public static InputStream readFromFile(Context context,String fileName) {
 InputStream ret=null;
try {
ret = context.openFileInput(fileName);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
return ret;
}
 public static boolean fileExists(Context context, String filename) {    
   File file = context.getFileStreamPath(filename);
   if(file == null || !file.exists()) {
       return false;
   }
   return true;
}
 

  • 7*12小时售后支持
  • 30+纯技术开发团队
  • 1000+客户选择
  • 7200小时稳定运营

Copyright © 2015-2022 hzappkf.com 杭州杭谐科技有限公司 版权所有 浙ICP备17012630号-1

微信咨询
回到顶部
在线咨询