Code example for WebView: Displaying web pages

Methods: addJavascriptInterfacegetSettingsloadUrlsetScrollBarStylesetWebViewClient, setScrollContainer

0
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		sp=this.getSharedPreferences("CALLTIME", 0);
		handler = new Handler();
		webView = (WebView) this.findViewById(R.id.webView);
		webSettings = webView.getSettings();
		//设置滚动条背景  
		webView.setScrollContainer(true);
		webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY );
		//设置了之后 页面展示有问题 
		//webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
		webSettings.setJavaScriptEnabled(true);
		webView.loadUrl("file:///android_asset/total.html");
		 
		webView.setWebViewClient(new WebViewClient() {
			@Override 
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true; 
			} 
		}); 
		//获取播出电话记录列表 
		webView.addJavascriptInterface(new Object() {
			public void getCallList(final String callnumber) {
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback2(" + getCallTimeListJson("type=2 and duration !=0 and number='"+callnumber+"'")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_outlist"); 
		//获取播出电话记录 
		webView.addJavascriptInterface(new Object() {
			public void getOutgoing() { 
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback(" + getCallTimeJson("type=2 and duration !=0 ) group by (number")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_out"); 
		//获取接听电话记录 
		webView.addJavascriptInterface(new Object() {
			public void getIncoming() { 
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback(" + getCallTimeJson("type=1 and duration !=0 ) group by (number")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_in"); 
		//获取接听电话记录列表 
		webView.addJavascriptInterface(new Object() {
			public void getCallList(final String callnumber) {
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback2(" + getCallTimeListJson("type=1 and duration !=0 and number='"+callnumber+"'")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_inlist"); 
		//获取未接电话记录 
		webView.addJavascriptInterface(new Object() {
			public void getMissing() { 
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback(" + getCallTimeJson("type=3 ) group by (number")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_miss"); 
		//获取接听电话记录列表 
		webView.addJavascriptInterface(new Object() {
			public void getCallList(final String callnumber) {
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback2(" + getCallTimeListJson("type=3 and number='"+callnumber+"'")
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_misslist"); 
		//获取电话记录总计 
		webView.addJavascriptInterface(new Object() {
			public void getTotal() { 
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("javascript:callback(" + getCallTimeTotal()
								+ ");"); 
					} 
				}); 
			} 
		}, "calltime_total"); 
		//设置套餐分钟数 
		webView.addJavascriptInterface(new Object() {
			public void setTotal(String month_total) {
				Editor editor=sp.edit();
				Integer m =0;
				try { 
					m= Integer.parseInt(month_total);
				} catch (Exception e) {
					m=200;
				} 
				//Log.i(TAG, "month_total-"+month_total); 
				editor.putInt("month_total",m );
				editor.commit();
				handler.post(new Runnable() {
					public void run() { 
						webView.loadUrl("file:///android_asset/total.html");
					} 
				}); 
			} 
		}, "set_calltime_total"); 
		//设置过滤的免费电话 
		webView.addJavascriptInterface(new Object() {
			public void setFreePhone(String phone_number) {
				Editor editor=sp.edit();
				//Log.i(TAG, "phone_number-"+phone_number.trim()); 
				editor.putString("free_phone_number",phone_number.trim());
				editor.commit();
Experience pair programming with AI  Get Codota for Java