package
com.rong05.webview;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
android.annotation.SuppressLint;
import
android.app.Activity;
import
android.content.Intent;
import
android.graphics.Bitmap;
import
android.os.Bundle;
import
android.util.FloatMath;
import
android.view.KeyEvent;
import
android.view.Menu;
import
android.view.MotionEvent;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.webkit.ValueCallback;
import
android.webkit.WebChromeClient;
import
android.webkit.WebSettings;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.FrameLayout;
import
android.widget.ProgressBar;
public
class
WebMainActivity
extends
Activity {
Button
go,next,last,history;
EditText
input;
WebView
web;
ProgressBar
progress;
static
double
beforeLenght=
0 ,changeLenght= 0 ;
static
String FILE_NAME=
"history.txt" ;
@SuppressLint ( "SetJavaScriptEnabled" )
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_web_main);
findView();
web.loadUrl( "http://baidu.com" );
web.setWebViewClient(client);
WebSettings
setter=web.getSettings();
//实现JavaScrip
setter.setJavaScriptEnabled( true );
//实现网页的缩放
setter.setBuiltInZoomControls( true );
setter.setJavaScriptCanOpenWindowsAutomatically( true );
setter.setGeolocationEnabled( true );
web.setWebChromeClient(chromeClient);
go.setOnClickListener( new
Action());
next.setOnClickListener( new
Action());
last.setOnClickListener( new
Action());
history.setOnClickListener( new
Action());
if (WebMainActivity.changeLenght>WebMainActivity.beforeLenght)
web.zoomIn();
else
if (WebMainActivity.changeLenght<WebMainActivity.beforeLenght)
web.zoomOut();
}
/**
*
对WebView的实现其中的方法
*
*/
WebViewClient
client= new
WebViewClient(){
//该方法是让网页在WebView中显示不调用本地浏览器
@Override
public
boolean
shouldOverrideUrlLoading(WebView view, String url) {
//
TODO Auto-generated method stub
return
super
.shouldOverrideUrlLoading(view,
url);
}
//对历史记录的刷新
@Override
public
void
doUpdateVisitedHistory(WebView view, String url,
boolean
isReload) {
//
TODO Auto-generated method stub
last.setEnabled(web.canGoBack());
next.setEnabled(web.canGoForward());
super .doUpdateVisitedHistory(view,
url, isReload);
}
//在读完网页后对其进行的操作
@Override
public
void
onPageFinished(WebView view, String url) {
//设置程序的标题为网页的标题
if
(web.getTitle() !=
null )
{
WebMainActivity. this .setTitle(web.getTitle());
getTitles(web.getTitle());
getHistory(url);
}
}
//在开始读取网页时对其进行的操作
@Override
public
void
onPageStarted(WebView view, String url, Bitmap favicon) {
//
TODO Auto-generated method stub
WebMainActivity. this .setTitle( "Loading..." );
last.setEnabled(web.canGoBack());
next.setEnabled(web.canGoForward());
super .onPageStarted(view,
url, favicon);
}
};
/**
*
对WebView的实现其中 WebChromeClient的方法
*/
WebChromeClient
chromeClient= new
WebChromeClient(){
//实现进度条
@Override
public
void
onProgressChanged(WebView view,
int
newProgress) {
//
TODO Auto-generated method stub
super .onProgressChanged(view,
newProgress);
progress.setProgress(newProgress);
}
//对历史记录的刷新
@Override
public
void
getVisitedHistory(ValueCallback<String[]> callback) {
//
TODO Auto-generated method stub
super .getVisitedHistory(callback);
}
};
//实现Button的按钮的动作
public
class
Action
implements
OnClickListener{
@Override
public
void
onClick(View v) {
//
TODO Auto-generated method stub
switch (v.getId()){
case
R.id.go:
WebMainActivity. this .web.loadUrl(WebMainActivity. this .input.getText().toString().trim());
WebMainActivity. this .input.setText( "http://" );
break ;
case
R.id.last:
if (WebMainActivity. this .web.canGoBack())
WebMainActivity. this .web.goBack();
else
v.setClickable( false );
break ;
case
R.id.next:
if (WebMainActivity. this .web.canGoForward())
WebMainActivity. this .web.goForward();
else
v.setClickable( false );
break ;
case
R.id.history:
//跳转到History页面
Intent
intent= new
Intent(getApplicationContext(),History.
class );
WebMainActivity. this .startActivityForResult(intent,
1 );
break ;
default :
break ;
}
}
}
/**
*
实现触摸事件
*/
@Override
public
boolean
onTouchEvent(MotionEvent event) {
//
TODO Auto-generated method stub
if (event.getHistorySize()== 2 )
switch (event.getAction())
{
case
MotionEvent.ACTION_DOWN:
WebMainActivity.beforeLenght=Spacing(event);
case
MotionEvent.ACTION_UP:
WebMainActivity.changeLenght=Spacing(event);
default :
break ;
}
return
super
.onTouchEvent(event);
}
/**
*
计算两指的距离
*
@param event
*
@return
*/
private
float
Spacing(MotionEvent event) {
float
x = event.getX(
0 )
- event.getX( 1 );
float
y = event.getY(
0 )
- event.getY( 1 );
return
FloatMath.sqrt(x * x + y * y);
}
//findViewById实现布局中的控件
public
void
findView(){
go=(Button)findViewById(R.id.go);
next=(Button)findViewById(R.id.next);
last=(Button)findViewById(R.id.last);
history=(Button)findViewById(R.id.history);
input=(EditText)findViewById(R.id.inputText);
web=(WebView)findViewById(R.id.web);
progress=(ProgressBar)findViewById(R.id.progress);
}
//得到历史信息写入history.txt
public
void
getHistory(String url){
FileOutputStream
fos = null ;
try
{
fos=openFileOutput(FILE_NAME,
MODE_APPEND);
try
{
fos.write((url+ ";" ).getBytes());
}
catch
(IOException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
catch
(FileNotFoundException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fos!= null ){
try
{
fos.flush();
fos.close();
}
catch
(IOException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//得到网页的标题写入title.txt
public
void
getTitles(String url){
FileOutputStream
fos = null ;
try
{
fos=openFileOutput( "title.txt" ,
MODE_APPEND);
try
{
fos.write((url+ ";" ).getBytes());
}
catch
(IOException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
catch
(FileNotFoundException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (fos!= null ){
try
{
fos.flush();
fos.close();
}
catch
(IOException e) {
//
TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
*
对History的Activity的返回信息进行操作
*/
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
//
TODO Auto-generated method stub
switch (resultCode){
case
RESULT_OK:
web.loadUrl(data.getStringExtra( "retmsg" ));
break ;
case
RESULT_CANCELED:
break ;
default :
break ;
}
}
//对返回键的重写
@Override
public
boolean
onKeyDown(
int
keyCode, KeyEvent event) {
//
TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_BACK){
if (web.canGoBack()){
web.goBack();
return
true
;
}
else {
System.exit( 0 );
}
}
return
super
.onKeyDown(keyCode,
event);
}
@Override
protected
void
onPause() {
//
TODO Auto-generated method stub
web.pauseTimers();
if (isFinishing()){
web.loadUrl( "about:blank" );
setContentView( new
FrameLayout(
this ));
}
super .onPause();
}
@Override
protected
void
onResume() {
//
TODO Auto-generated method stub
super .onResume();
web.resumeTimers();
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
//
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.web_main,
menu);
return
true
;
}
}
|