• 通过android系统的WebView控件访问web应用


    主程序入口

     1 public class GwpActivity extends Activity {
     2 
     3     private WebView webView;
     4 
     5     @Override
     6     public void onCreate(Bundle savedInstanceState) {
     7         super.onCreate(savedInstanceState);
     8         setContentView(R.layout.main);
     9         init();
    10     }
    11 
    12     public void init() {
    13         webView = (WebView) findViewById(R.id.gwkview);
    14         webView.getSettings().setJavaScriptEnabled(true);
    15         webView.setWebViewClient(new GwkWebViewClient());
    16 
    17         InputStream in = getResources().openRawResource(R.raw.gwkinfo);  // 读取res/raw/gwkinfo属性文件内容
    18         Properties p = new Properties();
    19         try {
    20             p.load(in);
    21             webView.loadUrl(p.getProperty("domain_url", "http://xxxx.com"));
    22         } catch (Exception e) {
    23             webView.loadUrl("http://xxxx.com");
    24         } finally {
    25             try {
    26                 in.close();
    27             } catch (IOException e) {
    28             }
    29         }
    30     }
    31 
    32     @Override
    33     public boolean onKeyDown(int keyCode, KeyEvent event) {
    34         if(webView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    35             webView.goBack();
    36             return true;
    37         }
    38         return super.onKeyDown(keyCode, event);
    39     }
    40 
    41     @Override
    42     protected void onDestroy() {
    43         super.onDestroy();
    44         System.exit(0);
    45     }
    46 }

     自定义WebView

    public class GwkWebViewClient extends WebViewClient {
    
    /*
    * 需要区分点击来源, 所以在请求地址上加了个参数用来标识点击是从手机上来的.
    */ @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { int position = url.indexOf("?"); if (position == -1) { url = url + "?type=app"; } else { url = url + "&type=app"; } view.loadUrl(url); return true; }
    /**
    *
    * 当接收出错的时候, 显示自定义的错误页面.
    */ @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { view.loadUrl("file:///android_asset/error.html"); // 加载assets/error.html页面 } }
  • 相关阅读:
    [ solr入门 ] 在schema.xml中加入自己的分词工具
    SQLServer2005获取大数据集时内存不足的解决办法[转]
    java位操作基本教程[转]
    log4j的最佳实践(转)
    [ lucene扩展 ] "Did you mean" feature with Apache Lucene SpellChecker
    java image filters[02]过滤器初探
    PHP serialize 和 JSON 解析与区别
    js 实现 静态缓存页面中访问动态IP下载地址
    smarty section foreach遍历多维数组
    【转】window.open 参数
  • 原文地址:https://www.cnblogs.com/makar/p/3295471.html
Copyright © 2020-2023  润新知