zoukankan      html  css  js  c++  java
  • 通过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页面 } }
  • 相关阅读:
    POJ 3114 Tarjan+Dijkstra
    278. First Bad Version
    209. Minimum Size Subarray Sum
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    710. Random Pick with Blacklist
    767. Reorganize String
    524. Longest Word in Dictionary through Deleting
    349. Intersection of Two Arrays
    350. Intersection of Two Arrays II
  • 原文地址:https://www.cnblogs.com/makar/p/3295471.html
Copyright © 2011-2022 走看看